[From] https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000
Spring Boot Configuration multiple datasource
Liaoche/Programming/ 1-13 10:11/read: 14041
With spring boot, the configuration is easy by default DataSource
. Spring boot will automatically configure good one for us DataSource
.
If a application.yml
related configuration is specified in spring.datasource
, Spring boot uses that configuration to create one DataSource
. If application.yml
no related configuration is specified in spring.datasource
, Spring boot will search the classpath for the jar packages of memory databases such as H2, HSQLDB, and, if found, automatically configure a memory database DataSource
, so We just need to introduce the jar package. For example, configure a HSQLDB data source:
<dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope></dependency>
However, in some cases, if we need to configure multiple data sources, how should we configure them in spring boot?
We use JDBC as an example to demonstrate how to configure two in spring boot DataSource
. Correspondingly, we will create two JdbcTemplate
beans, each using these two data sources.
First, we must application.yml
declare the configuration of two data sources in, one using spring.datasource
, the other using spring.second-datasource
:
SpringApplication:Namedata-MultidatasourceDataSourcedriver-class-name: org.hsqldb.jdbc url: jdbc:hsqldb:mem< span class= "pseudo" >:d B1 username: sa password: second-datasource: driver-class-name: org.hsqldb.jdbc . Jdbcdriver url: jdbc:hsqldb:mem< span class= "pseudo" >:d B2 username: sa password:
DataSource
all two use HSQLDB, but the database is different. In addition, all necessary configurations cannot be omitted when using multiple data sources.
Second, we need to create two of our own DataSource
beans, one of which is labeled @Primary
, and the other named secondDatasource
:
@Configuration public class someconfiguration { @Bean @Primary @ConfigurationProperties (prefix = "Spring.datasource") public DataSource primarydatasource () {return datasourcebuilder.create (). build (); } @Bean (name = @ Configurationproperties (prefix = "Spring.second-datasource") public DataSource Seconddatasource () {return datasourcebuilder.create (). build ();}}
For each one DataSource
, we must @ConfigurationProperties(prefix = "xxx")
prefix by specifying the configuration item.
Immediately after that, we created two JdbcTemplate
beans, one of which was labeled, the @Primary
other named secondJdbcTemplate
, and each used the corresponding DataSource
:
@Bean@Primarypublic JdbcTemplate primaryJdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource);}@Bean(name = "secondJdbcTemplate")public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDatasource") DataSource dataSource) { return new JdbcTemplate(dataSource);}
Notice that secondJdbcTemplate
at the time of creation, the incoming DataSource
must be @Qualifier("secondDatasource")
declared so that the second can be used DataSource
.
Now, we've created two of these JdbcTemplate
Bean
. Where we need to use JdbcTemplate
the first one, we inject directly:
@Componentpublic class SomeService { @Autowired JdbcTemplate jdbcTemplate;}
When we need to use JdbcTemplate
the second place, we need to identify the injection @Qualifier("secondJdbcTemplate")
:
@Componentpublic class AnotherService { @Autowired @Qualifier("secondJdbcTemplate") JdbcTemplate secondJdbcTemplate;}
In this way, we can operate on different data sources and in different ways JdbcTemplate
.
Precautions
When there are more than one bean of the same type, for example, multiple, multiple, it is DataSource
JdbcTemplate
strongly recommended to always use one of the @Primary
beans identified as "primary", using @Autowired
injection to first use the bean that is marked @Primary
.
Other beans of the same type, each of which needs to be @Bean(name="xxx")
identified by name, and the @Autowired
name of the @Qualifier("xxx")
injected Bean is specified with the injection.
For a complete sample project source code, please refer to:
Https://github.com/michaelliao/springcloud/tree/master/data-multidatasource
[Go] Spring boot configuration multiple datasource