基于SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置數(shù)據(jù)庫的加載
1. 背景
現(xiàn)項(xiàng)目有一個(gè)需求,期望通過在application.yml
配置文件中設(shè)置一個(gè)開關(guān),來決定是否加載數(shù)據(jù)庫。要求:
當(dāng)開關(guān)的值為
true
時(shí)加載數(shù)據(jù)庫;當(dāng)開關(guān)的值為
false
或沒有該配置時(shí)則不加載數(shù)據(jù)庫。
2. 具體實(shí)現(xiàn)
2.1 數(shù)據(jù)庫相關(guān)配置
application.yml
中添加配置spring.datasource.enabled
來決定是否加載數(shù)據(jù)庫相關(guān)配置。
spring: datasource: #開啟MySQL enabled: true driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf8&connectTimeout=30000&socketTimeout=30000&autoReconnect=true&cachePrepStmts=true&useServerPrepStmts=true username: root password: 123456 type: com.zaxxer.hikari.HikariDataSource hikari: # 連接池名稱 pool-name: MyHikariCP #最小空閑連接,默認(rèn)值10,小于0或大于maximum-pool-size,都會(huì)重置為maximum-pool-size minimum-idle: 10 #連接池最大連接數(shù),默認(rèn)是10 (cpu核數(shù)量 * 2 + 硬盤數(shù)量) maximum-pool-size: 30 #空閑連接超時(shí)時(shí)間,默認(rèn)值600000(10分鐘),大于等于max-lifetime且max-lifetime>0,會(huì)被重置為0;不等于0且小于10秒,會(huì)被重置為10秒。 idle-timeout: 600000 #連接最大存活時(shí)間,不等于0且小于30秒,會(huì)被重置為默認(rèn)值30分鐘.設(shè)置應(yīng)該比mysql設(shè)置的超時(shí)時(shí)間短 max-lifetime: 1800000 #連接超時(shí)時(shí)間:毫秒,小于250毫秒,否則被重置為默認(rèn)值30秒 connection-timeout: 30000 #用于測(cè)試連接是否可用的查詢語句 connection-test-query: SELECT 1 jpa: database: mysql show-sql: false hibernate: naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl ddl-auto: update properties: hibernate: jdbc: batch_size: 100 dialect: org.hibernate.dialect.MySQL5InnoDBDialect enable_lazy_load_no_trans: true open-in-view: false
2.2 啟動(dòng)類添加注解
啟動(dòng)類添加注解@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
, 代表默認(rèn)情況下不自動(dòng)加載數(shù)據(jù)庫相關(guān)配置。
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class OcrsrvcApplication { public static void main(String[] args) { SpringApplication.run(OcrsrvcApplication.class, args); } }
2.3 新增DataSourceConfig配置類
新增數(shù)據(jù)源配置類,用于自定義數(shù)據(jù)源的創(chuàng)建。
使用注解@ConditionalOnProperty
,這樣可以根據(jù)特定屬性的值來決定是否加載某個(gè)組件。
@Configuration @ConditionalOnProperty(name = "spring.datasource.enabled", havingValue = "true") @Slf4j public class DataSourceConfig { @Value("${spring.datasource.url}") private String jdbcUrl; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.hikari.pool-name}") private String poolName; @Value("${spring.datasource.hikari.minimum-idle}") private int minimumIdle; @Value("${spring.datasource.hikari.maximum-pool-size}") private int maximumPoolSize; @Value("${spring.datasource.hikari.idle-timeout}") private long idleTimeout; @Value("${spring.datasource.hikari.max-lifetime}") private long maxLifetime; @Value("${spring.datasource.hikari.connection-timeout}") private long connectionTimeout; @Value("${spring.datasource.hikari.connection-test-query}") private String connectionTestQuery; @Bean public DataSource dataSource() { log.info("====加載數(shù)據(jù)庫==="); HikariConfig hikariConfig = new HikariConfig(); hikariConfig.setJdbcUrl(jdbcUrl); hikariConfig.setDriverClassName(driverClassName); hikariConfig.setUsername(username); hikariConfig.setPassword(password); hikariConfig.setPoolName(poolName); hikariConfig.setMinimumIdle(minimumIdle); hikariConfig.setMaximumPoolSize(maximumPoolSize); hikariConfig.setIdleTimeout(idleTimeout); hikariConfig.setMaxLifetime(maxLifetime); hikariConfig.setConnectionTimeout(connectionTimeout); hikariConfig.setConnectionTestQuery(connectionTestQuery); return new HikariDataSource(hikariConfig); } }
2.4 相關(guān)業(yè)務(wù)邏輯調(diào)整
涉及到使用數(shù)據(jù)庫的相關(guān)業(yè)務(wù)邏輯,可在XxxRepository
類上添加注解@ConditionalOnProperty(name = "spring.datasource.enabled", havingValue = "true")
,這代表只有在啟用數(shù)據(jù)庫時(shí),才會(huì)加載相應(yīng)組件。
使用注解@Autowired(required = false)
注入XxxRepository
,代表允許為空,避免Bean不存在時(shí)出錯(cuò)
@Autowired(required = false) private XxxRepository xxxRepository;
到此這篇關(guān)于基于SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置數(shù)據(jù)庫的加載的文章就介紹到這了,更多相關(guān)SpringBoot配置數(shù)據(jù)庫的加載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Spring boot的項(xiàng)目中使用Junit進(jìn)行單體測(cè)試
今天小編就為大家分享一篇關(guān)于spring boot使用Junit進(jìn)行測(cè)試,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Java樹形結(jié)構(gòu)數(shù)據(jù)生成導(dǎo)出excel文件方法記錄
最近好像得罪了poi,遇到的都是導(dǎo)出word、Excel、pdf的問題,下面這篇文章主要給大家介紹了關(guān)于Java樹形結(jié)構(gòu)數(shù)據(jù)生成導(dǎo)出excel文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程
這篇文章主要介紹了Java的Socket網(wǎng)絡(luò)編程基礎(chǔ)知識(shí)入門教程,包括基于TCP/IP和UDP協(xié)議的簡(jiǎn)單實(shí)例程序講解,需要的朋友可以參考下2016-01-01java之Timer和TimerTask簡(jiǎn)單demo(分享)
下面小編就為大家?guī)硪黄猨ava之Timer和TimerTask簡(jiǎn)單demo(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析
這篇文章主要介紹了Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java實(shí)現(xiàn)SMS短信通發(fā)送手機(jī)驗(yàn)證碼案例講解
這篇文章主要介紹了Java實(shí)現(xiàn)SMS短信通發(fā)送手機(jī)驗(yàn)證碼案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析
這篇文章主要介紹了springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09