1. Data source configuration
Three data source types, Tomcat, HikariCP, and Commons DBCP are configured by default in Spring Boot. If they can be found in the classpath, Tomcat will be loaded first (sequential loading)
Configuration method
@Bean(name="dataSource", initMethod="init", destroyMethod="close") public DataSource dataSource() { return DataSourceBuilder.create().build(); }
The properties prefixed with spring.datasource are used by default, and you can also customize properties and read them through @Value
spring.datasource.url=jdbc:mysql://localhost:3306/db1 spring.datasource.username=root spring.datasource.password=123456
2. Use Alibaba's open source druid data source configuration
Introduce pom dependencies, and automatically prompt druid configuration properties in the configuration file after introduction
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.2</version> </dependency>
When configuring a single data source, just replace the creation of the data source
return DruidDataSourceBuilder.create().build();
Druid-based multi-data source configuration
@Bean(name="dataSource", initMethod="init", destroyMethod="close")//别名指定 @Primary //designated as the main data source @ConfigurationProperties("spring.datasource.druid") //Specify the property reference in the configuration file public DataSource dataSource() { return DruidDataSourceBuilder.create().build(); } /***Configure other data sources***/ @Bean(name="otherDataSource", initMethod="init", destroyMethod="close") @ConfigurationProperties("other.datasource.druid") //Specify the property reference in the configuration file public DataSource otherDataSource() { return DruidDataSourceBuilder.create().build(); }
define configuration file
spring.datasource.druid.url=jdbc:mysql://localhost:3306/db1 spring.datasource.druid.username=root spring.datasource.druid.password=123456 spring.datasource.druid.filters=stat,wall,slf4j //druid monitoring configuration #Custom data source property configuration, druid-spring-boot-starter has introduced druid configuration prompts, just redefine according to the prompts #Attribute prefix is enough to realize personalized configuration of different data sources other.datasource.druid.type=com.alibaba.druid.pool.DruidDataSource other.datasource.druid.url=jdbc:mysql://localhost:3306/db2 other.datasource.druid.username=root other.datasource.druid.password=123456 other.datasource.druid.filters=stat,wall,slf4j
The injection of the data source in jdbctemple, the use of the data source is distinguished by the @Qualifier annotation
@Bean public JdbcTemplate jdbcTemplate(@Qualifier("dataSource")DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean("otherJdbcTemplate") public JdbcTemplate otherJdbcTemplate(@Qualifier("otherDataSource")DataSource dataSource) { return new JdbcTemplate(dataSource); }
View the data source initialization information on the console, and you can see that both have been initialized
--- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-2} inited
Druid monitoring console view
http://localhost:8765/druid/datasource.html
数据源1,Basic Info For DataSource-1920147706 数据源2,Basic Info For DataSource-2034046523