當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
hikari数据源配置类_Spring中的“多数据源”之详解
生活随笔
收集整理的這篇文章主要介紹了
hikari数据源配置类_Spring中的“多数据源”之详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
注意事項(xiàng)
- 有多個(gè)DataSource時(shí)系統(tǒng)如何判斷
- 對(duì)應(yīng)的設(shè)施(事務(wù)、ORM)如何選擇DataSource
多數(shù)據(jù)源配置(二選一)
- DataSourceAutoConfiguration
- DataSourceTransactionManagerAutoConfiguration
- JdbcTemplateAutoConfiguration
依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope> </dependency>application.properties
management.endpoints.web.exposure.include=* foo.datasource.url=jdbc:h2:mem:foo foo.datasource.username=SA foo.datasource.password= bar.datasource.url=jdbc:h2:mem:bar bar.datasource.username=SA bar.datasource.password=DataSourceConfig
@Slf4j @Configuration public class DataSourceConfig {@Bean@ConfigurationProperties("foo.datasource")public DataSourceProperties fooDataSourceProperties() {return new DataSourceProperties();}@Beanpublic DataSource fooDataSource() {DataSourceProperties dataSourceProperties = fooDataSourceProperties();log.info("foo datasource : {}", dataSourceProperties.getUrl());return dataSourceProperties.initializeDataSourceBuilder().build();}@Bean@Resourcepublic PlatformTransactionManager fooTxManager(DataSource fooDataSource) {return new DataSourceTransactionManager(fooDataSource);}@Bean@ConfigurationProperties("bar.datasource")public DataSourceProperties barDataSourceProperties() {return new DataSourceProperties();}@Beanpublic DataSource barDataSource() {DataSourceProperties dataSourceProperties = barDataSourceProperties();log.info("bar datasource : {}", dataSourceProperties.getUrl());return dataSourceProperties.initializeDataSourceBuilder().build();}@Bean@Resourcepublic PlatformTransactionManager barTxManager(DataSource barDataSource) {return new DataSourceTransactionManager(barDataSource);} }MultiDatasourceApplication
// 排查自動(dòng)配置 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JdbcTemplateAutoConfiguration.class}) public class MultiDatasourceApplication {public static void main(String[] args) {SpringApplication.run(MultiDatasourceApplication.class, args);} }beans
"fooDataSource": {"aliases": [],"scope": "singleton","type": "com.zaxxer.hikari.HikariDataSource","resource": "class path resource [me/zhongmingmao/multidatasource/DataSourceConfig.class]","dependencies": ["fooDataSourceProperties"] } "fooTxManager": {"aliases": [],"scope": "singleton","type": "org.springframework.jdbc.datasource.DataSourceTransactionManager","resource": "class path resource [me/zhongmingmao/multidatasource/DataSourceConfig.class]","dependencies": ["fooDataSource"] }小結(jié)
最后,小架在這里祝大家國慶快樂!
希望一直以來關(guān)注我的各位越來越好,身體健康,學(xué)業(yè)有成。在成為架構(gòu)師的這條路上,我以后也會(huì)一直陪大家走下去! 會(huì)努力的去尋找更多的新內(nèi)容新知識(shí),來分享給大家,接下來可能會(huì)脫更幾天,因?yàn)槟承┎豢煽沽?#xff0c;再次感謝大家的看我的文章!
仰天大笑出門去,我輩豈是蓬蒿人。
我是小架,我們
下篇文章見!
總結(jié)
以上是生活随笔為你收集整理的hikari数据源配置类_Spring中的“多数据源”之详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 股票发行面值和价格有什么区别
- 下一篇: 一加 27W 冰点散热背夹“凌风青”将随