Spring boot 随笔 1 DatasourceInitializer

0. 为啥感觉升级了 win11 之后,电脑像是刚买回来的,很快

这篇加餐完全是一个意外:时隔两年半,再看 Springboot-quartz-starter 集成实现的时候,不知道为啥我的h2 在应用启动的时候,不能自动创建quartz相关的schema。后面看了 springboot 的文档,据说是可以做到的,AI也是这么说的。

没办法,只能看 QuartzAutoConfiguration 源码了。于是乎,就有了这么个好活

没办法,就当是一个支线任务了

1. AbstractScriptDatabaseInitializer

请添加图片描述

下面是熟悉的,阉割后的 源码

package org.springframework.boot.sql.init;/*** Base class for an {@link InitializingBean} that performs SQL database initialization* using schema (DDL) and data (DML) scripts.** @author Andy Wilkinson* @since 2.5.0*/
public abstract class AbstractScriptDatabaseInitializer implements ResourceLoaderAware, InitializingBean {// 构造入参配置private final DatabaseInitializationSettings settings;private volatile ResourceLoader resourceLoader;@Overridepublic void afterPropertiesSet() throws Exception {// 初始化后,就执行逻辑了initializeDatabase();}/*** Initializes the database by applying schema and data scripts.* @return {@code true} if one or more scripts were applied to the database, otherwise* {@code false}*/public boolean initializeDatabase() {ScriptLocationResolver locationResolver = new ScriptLocationResolver(this.resourceLoader);// 先后执行 schema, data 的脚本boolean initialized = applySchemaScripts(locationResolver);return applyDataScripts(locationResolver) || initialized;}// 真正执行脚本前,会走这个判断,决定是否要执行脚本private boolean isEnabled() {if (this.settings.getMode() == DatabaseInitializationMode.NEVER) {return false;}return this.settings.getMode() == DatabaseInitializationMode.ALWAYS || isEmbeddedDatabase();}/*** Returns whether the database that is to be initialized is embedded.* @return {@code true} if the database is embedded, otherwise {@code false}* @since 2.5.1*/protected boolean isEmbeddedDatabase() {throw new IllegalStateException("Database initialization mode is '" + this.settings.getMode() + "' and database type is unknown");}private boolean applySchemaScripts(ScriptLocationResolver locationResolver) {return applyScripts(this.settings.getSchemaLocations(), "schema", locationResolver);}private boolean applyDataScripts(ScriptLocationResolver locationResolver) {return applyScripts(this.settings.getDataLocations(), "data", locationResolver);}private boolean applyScripts(List<String> locations, String type, ScriptLocationResolver locationResolver) {List<Resource> scripts = getScripts(locations, type, locationResolver);if (!scripts.isEmpty() && isEnabled()) {runScripts(scripts);return true;}return false;}// 根据配置的 路径的字符串 -> spring.Resource 类型private List<Resource> getScripts(List<String> locations, String type, ScriptLocationResolver locationResolver) {if (CollectionUtils.isEmpty(locations)) {return Collections.emptyList();}List<Resource> resources = new ArrayList<>();for (String location : locations) {for (Resource resource : doGetResources(location, locationResolver)) {if (resource.exists()) {resources.add(resource);}}}return resources;}private List<Resource> doGetResources(String location, ScriptLocationResolver locationResolver) {return locationResolver.resolve(location);}private void runScripts(List<Resource> resources) {runScripts(resources, this.settings.isContinueOnError(), this.settings.getSeparator(),this.settings.getEncoding());}protected abstract void runScripts(List<Resource> resources, boolean continueOnError, String separator,Charset encoding);private static class ScriptLocationResolver {private final ResourcePatternResolver resourcePatternResolver;private List<Resource> resolve(String location) throws IOException {// ...}}}

再看几个它的实现类,加载上配置类,基本上,可以知道它的使用方法了

2. 吾のDemo

始于测试类

package org.pajamas.spring.boot;import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.pajamas.example.starter.core.entity.AlbumEntity;
import org.pajamas.example.starter.core.repo.AlbumRepo;
import org.pajamas.example.test.AbstractApplicationTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;import java.util.List;/*** @author william* @since 2024/5/30*/
@DisplayName("what the interesting component")
@TestPropertySource(properties = {"spring.application.name=service-example-test",// 屏蔽 liquibase 的干扰 "spring.liquibase.enabled=false"
})
@Import(ExampleDatabaseInitializer.class)
public class DatabaseInitializerTest extends AbstractApplicationTest {// 其实就,一个 jpa 实体类的 repository@AutowiredAlbumRepo repo;// @Disabled@DisplayName("execute DDL, DML automatically, as App startup")@Testpublic void t0() throws Exception {// 预期的结果:启动启动时,自动创建表,并插入一条记录List<AlbumEntity> all = this.repo.findAll();printErr(all);}
}

既然是测试,就走简单的方式,注册这个bean

package org.pajamas.spring.boot;import org.springframework.boot.autoconfigure.sql.init.SqlDataSourceScriptDatabaseInitializer;
import org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties;
import org.springframework.boot.sql.init.DatabaseInitializationMode;import java.util.Collections;import javax.sql.DataSource;/*** @author william* @since 2024/5/30*/
public class ExampleDatabaseInitializer extends SqlDataSourceScriptDatabaseInitializer {public ExampleDatabaseInitializer(DataSource dataSource) {super(dataSource, getProperty());}private static SqlInitializationProperties getProperty() {SqlInitializationProperties properties = new SqlInitializationProperties();properties.setSchemaLocations(Collections.singletonList("classpath:sql/schema.sql"));properties.setDataLocations(Collections.singletonList("classpath:sql/data.sql"));properties.setMode(DatabaseInitializationMode.ALWAYS);properties.setContinueOnError(false);return properties;}
}

schema.sql

CREATE TABLE IF NOT EXISTS `t_album`
(`id`             bigint NOT NULL AUTO_INCREMENT,`album_name`     varchar(32)                                                  DEFAULT NULL COMMENT 'album name',`album_year`     int                                                          DEFAULT NULL COMMENT 'album publish year',`create_date`    timestamp NULL DEFAULT NULL,`create_user_id` bigint                                                       DEFAULT NULL,`update_date`    timestamp NULL DEFAULT NULL,`update_user_id` bigint                                                       DEFAULT NULL,`ver`            int    NOT NULL                                              DEFAULT '0',`del`            bigint NOT NULL                                              DEFAULT '0',PRIMARY KEY (`id`),UNIQUE KEY `uni_album_id_del` (`id`,`del`)
) COMMENT='album table';CREATE TABLE IF NOT EXISTS `t_artist`
(`id`             bigint NOT NULL AUTO_INCREMENT,`artist_name`    varchar(32)                                                  DEFAULT NULL COMMENT 'artist name',`artist_from`    varchar(32)                                                  DEFAULT NULL COMMENT 'shorten of country name',`create_date`    timestamp NULL DEFAULT NULL,`create_user_id` bigint                                                       DEFAULT NULL,`update_date`    timestamp NULL DEFAULT NULL,`update_user_id` bigint                                                       DEFAULT NULL,`ver`            int    NOT NULL                                              DEFAULT '0',`del`            bigint NOT NULL                                              DEFAULT '0',PRIMARY KEY (`id`),UNIQUE KEY `uni_artist_id_del` (`id`,`del`)
) COMMENT='artist table';

data.sql

insert into`t_album`
(`album_name`,`album_year`,`create_user_id`,`update_user_id`
)
values
('Boomerang',2023,1023,1023
);

3. 话说回来:为甚么,我的h2没有自动创建quartz的schema

这是springboot.Quartz的实现
在这里插入图片描述

接下来,源码启动…

package org.springframework.boot.jdbc.init;/*** {@link InitializingBean} that performs {@link DataSource} initialization using schema* (DDL) and data (DML) scripts.** @author Andy Wilkinson* @since 2.5.0*/
public class DataSourceScriptDatabaseInitializer extends AbstractScriptDatabaseInitializer {@Overrideprotected boolean isEmbeddedDatabase() {try {// step into ..return EmbeddedDatabaseConnection.isEmbedded(this.dataSource);}catch (Exception ex) {logger.debug("Could not determine if datasource is embedded", ex);return false;}}
}----------// org.springframework.boot.jdbc.EmbeddedDatabaseConnection/*** Convenience method to determine if a given data source represents an embedded* database type.* @param dataSource the data source to interrogate* @return true if the data source is one of the embedded types*/public static boolean isEmbedded(DataSource dataSource) {try {return new JdbcTemplate(dataSource)// step into ....execute(new IsEmbedded());}catch (DataAccessException ex) {// Could not connect, which means it's not embeddedreturn false;}}----------// org.springframework.boot.jdbc.EmbeddedDatabaseConnection.IsEmbedded@Overridepublic Boolean doInConnection(Connection connection) throws SQLException, DataAccessException {DatabaseMetaData metaData = connection.getMetaData();String productName = metaData.getDatabaseProductName();if (productName == null) {return false;}productName = productName.toUpperCase(Locale.ENGLISH);// step into ...EmbeddedDatabaseConnection[] candidates = EmbeddedDatabaseConnection.values();for (EmbeddedDatabaseConnection candidate : candidates) {if (candidate != NONE && productName.contains(candidate.getType().name())) {// 根据jdbc.url判断是不是一个 嵌入式数据库String url = metaData.getURL();return (url == null || candidate.isEmbeddedUrl(url));}}return false;}------------public enum EmbeddedDatabaseConnection {// H2 判断是否为嵌入式数据的依据/*** H2 Database Connection.*/H2(EmbeddedDatabaseType.H2, DatabaseDriver.H2.getDriverClassName(),"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", (url) -> url.contains(":h2:mem")),}

破案:我的h2 使用默认的 file(xxx.mv.db) 存储,默认配置下(DatabaseInitializationMode.EMBEDDED), 只有内存(嵌入式)的数据库会开启这个特性。

  • 要么配置 DatabaseInitializationMode.ALWAYS
  • 要么使用内存数据库

Anyway, h2支持好多种连接方式,新版本h2, 默认的file模式,采用mv的storeEngine 支持MVCC。所以说,对于quartz这种依赖行锁的要求,也是支持的。

4. 话又说回去… 这个东西对项目的意义是什么

  • 可以试下这个:如果你有一个连接数据库的测试环境,或者你的程序很简单,又或者 有特殊的xp(内存数据库)
  • 专门数据库的版本控制工具:你的程序比较复杂,或者 本身就需要数据库的版本控制工具(如 Liquibase),运行在严肃的生产环境

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/342233.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3项目炫酷实战,检测密码强度值

在前端项目开发中&#xff0c;确保用户密码的强度是保护账户安全的重要措施。本文将演示如何使用Vue 3实现一个简单的密码强度检测功能。通过实时反馈&#xff0c;帮助用户创建更安全的密码&#xff0c;从而提升整体系统的安全性。无论您是前端开发新手还是经验丰富的开发者&am…

Vue2指令

本节目标 掌握vue指令 定义常用指令案例-小黑记事本指令修饰符 介绍 指令就是带有v-前缀的标签属性, 不同的指令, 可以实现不同的功能 常用指令 渲染指令 语法: v-html 动态渲染标签作用: 动态设置元素的innerHTML场景: 用来动态解析标签 语法: v-text 动态渲染文本会…

气膜羽毛球馆如何提升运动体验—轻空间

随着人们对健康和运动的关注度日益增加&#xff0c;羽毛球作为一项受欢迎的运动&#xff0c;得到了越来越多人的喜爱。而气膜羽毛球馆&#xff0c;以其独特的优势&#xff0c;正在改变传统羽毛球馆的运动体验。那么&#xff0c;气膜羽毛球馆是如何提升运动体验的呢&#xff1f;…

神经网络 torch.nn---Non-Linear Activations (ReLU)

ReLU — PyTorch 2.3 documentation torch.nn - PyTorch中文文档 (pytorch-cn.readthedocs.io) 非线性变换的目的 非线性变换的目的是为神经网络引入一些非线性特征&#xff0c;使其训练出一些符合各种曲线或各种特征的模型。 换句话来说&#xff0c;如果模型都是直线特征的…

C#之EntityFramework的应用

目录 1&#xff0c;名词概述。 2&#xff0c;实体数据模型EDM介绍。 3&#xff0c;规范函数。 4&#xff0c;查看Linq转换成的SQL语句。 5&#xff0c;数据的增删改查。 5.1&#xff0c;数据查询 5.2&#xff0c;数据插入 5.3&#xff0c;数据更新 5.4&#xff0c;数据…

【android】设置背景图片

改变值&#xff0c;可显示zai在 在theves下面的两个value都要增加名字代码 <item name"windowActionBar">false</item><item name"android:windowNoTitle">true</item><item name"android:windowFullscreen">tru…

Base64前端图片乱码转换

title: Base64码乱转换 date: 2024-06-01 20:30:28 tags: vue3 后端图片前端显示乱码 现象 后端传来一个图片&#xff0c;前端能够接收&#xff0c;但是console.log()后发现图片变成了乱码&#xff0c;但是检查后台又发现能够正常的收到了这张图片。 处理方法 笔者有尝试将…

联想Y410P跑大模型

安装vs 2017 查看GPU版本 查看支持哪个版本的cuda windows cuda更新教程_cuda 12.0-CSDN博客 下载并安装cuda tookit 10.1 CUDA Toolkit 10.1 Update 2 Archive | NVIDIA Developer 找到下载的文件&#xff0c;安装 参考安装链接 Win10 Vs2017 CUDA10.1安装&#xff08;避坑…

【C语言】10.C语言指针(4)

文章目录 1.回调函数是什么&#xff1f;2.qsort 使⽤举例2.1 使⽤qsort函数排序整型数据2.2 使⽤qsort排序结构数据 3.qsort函数的模拟实现 1.回调函数是什么&#xff1f; 回调函数就是一个通过函数指针调用的函数。 如果你把函数的指针&#xff08;地址&#xff09;作为参数…

kafka-消费者-指定offset消费(SpringBoot整合Kafka)

文章目录 1、指定offset消费1.1、创建消费者监听器‘1.2、application.yml配置1.3、使用 Java代码 创建 主题 my_topic1 并建立3个分区并给每个分区建立3个副本1.4、创建生产者发送消息1.4.1、分区0中的数据 1.5、创建SpringBoot启动类1.6、屏蔽 kafka debug 日志 logback.xml1…

服务器数据恢复—EqualLogic存储硬盘灯亮黄色的数据恢复案例

服务器数据恢复环境&#xff1a; 一台某品牌EqualLogic PS 6011型号存储&#xff0c;底层有一组由16块SAS硬盘组建的RAID5阵列&#xff0c;上层存储空间划分了4个卷&#xff0c;格式化为VMFS文件系统&#xff0c;存放虚拟机文件。 服务器故障&#xff1a; 存储设备上两块硬盘指…

基于STC89C52单片机空气PM2.5系统设计资料

#include <reg52.h>#include <intrins.h>#define uint unsigned int#define uchar unsigned char //宏定义sbit RSP1^6;//液晶接口sbit ENP1^7;sbit LED P2^0;//粉尘传感器控制接口sbit ADCS P3^7;//AD0832接口sbit ADCLK P3^5;sbit ADDI P3^6;sbit ADDO P3^6;…

【大模型】基于Hugging Face调用及微调大模型(1)

文章目录 一、前言二、Transformer三、Hugging Face3.1 Hugging Face Dataset3. 2 Hugging Face Tokenizer3.3 Hugging Face Transformer3.4 Hugging Face Accelerate 四、基于Hugging Face调用模型4.1 调用示例4.2 调用流程概述4.2.1 Tokenizer4.2.2 模型的加载4.2.3 模型基本…

# RocketMQ 实战:模拟电商网站场景综合案例(二)

RocketMQ 实战&#xff1a;模拟电商网站场景综合案例&#xff08;二&#xff09; 一、SpringBoot 整合 Dubbo &#xff1a;dubbo 概述 1、dubbo 概述 Dubbo &#xff1a;是阿里巴巴公司开源的一款高性能、轻量级的 Java RPC 框架&#xff0c;它提供了三大核心能力&#xff1a…

【WP】猿人学15_备周则意怠_常见则不疑

https://match.yuanrenxue.cn/match/15 抓包分析 抓包分析有一个m参数&#xff0c;三个数字组成 追栈/扣代码 根据启动器顺序追栈&#xff0c;一般优先跳过 jQuery 直接能找到加密函数 每次获取的数字都不一样 window.m function() { t1 parseInt(Date.parse(new Date(…

【全开源】Java共享茶室棋牌室无人系统支持微信小程序+微信公众号

打造智能化休闲新体验 一、引言&#xff1a;智能化休闲时代的来临 随着科技的飞速发展&#xff0c;智能化、无人化服务逐渐渗透到我们生活的各个领域。在休闲娱乐行业&#xff0c;共享茶室棋牌室无人系统源码的出现&#xff0c;不仅革新了传统的休闲方式&#xff0c;更为消费…

Marin说PCB之如何在主板上补偿链路中的走线的等长误差?

一场雨把我困在这里&#xff0c;你冷漠地看我没有穿雨衣淋成落汤鸡。今天刚刚出门时候看天气预报没有雨&#xff0c;于是我就没有带雨衣骑电动车去公司了&#xff0c;谁知道回来的路上被淋成狗了。天气预报就像是女人的脾气那样&#xff0c;不能完全相信的。 好了&#xff0c;我…

什么是视频号招商团长?如何加入成为视频号招商团长

视频号招商团长&#xff0c;是通过微信视频号平台的线上和线下活动&#xff0c;撮合商家和达人进行合作&#xff0c;帮助商家、达人在视频号成长发展&#xff1b;同时还可以通过邀请内容创作者入驻微信视频号并为其提供支持&#xff1b;从而获取佣金收益的&#xff0c;而其作用…

【LeetCode算法】第100题:相同的树

目录 一、题目描述 二、初次解答 三、官方解法 四、总结 一、题目描述 二、初次解答 1. 思路&#xff1a;二叉树的先序遍历。采用递归的先序遍历方法&#xff0c;首先访问根节点若不同则返回false&#xff0c;其次访问左子树和右子树。在访问左右子树时&#xff0c;需要注意…

CAN总线学习笔记-CAN帧结构

数据帧 数据帧&#xff1a;发送设备主动发送数据&#xff08;广播式&#xff09; 标准格式的11ID不够用了&#xff0c;由此产生了扩展格式 SOF&#xff1a;帧起始&#xff0c;表示后面一段波形为传输的数据位 ID&#xff1a;标识符&#xff0c;区分功能&#xff0c;同时决定优…