配置流程
(一)配置文件配置
#多源數(shù)據(jù)庫(kù)配置
#第一數(shù)據(jù)庫(kù)
spring.datasource.primary.url=jdbc:MySQL://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=123456
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
#第二數(shù)據(jù)庫(kù)
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/spring1?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=123456
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
(二)新建一個(gè)DataSourceConfig配置類(lèi),代碼如下:
/**
* 多數(shù)據(jù)源配置
*/
@Configuration
public class DataSourceConfig {
/**
* 第一數(shù)據(jù)源
* @return
*/
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 第二數(shù)據(jù)源
* @return
*/
@Primary //默認(rèn)選擇這個(gè)數(shù)據(jù)源進(jìn)行執(zhí)行
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 本例使用的是jdbcTemplate來(lái)進(jìn)行測(cè)試,所以添加jdbcTemplate的支持如下
* @param dataSource
* @return
*/
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
(三)新建測(cè)試用例
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate JdbcTemplate2;
@Test
public void test() throws Exception {
//往第一數(shù)據(jù)庫(kù)添加數(shù)據(jù)
jdbcTemplate1.update("insert into user_test(name,age) values(?, ?)","多源數(shù)據(jù)庫(kù)",150);
//往第二數(shù)據(jù)庫(kù)添加數(shù)據(jù)
JdbcTemplate2.update("insert into user_test_1(name,age) values(?, ?)","多源數(shù)據(jù)庫(kù)",150);
}
(四)報(bào)錯(cuò)
JAVA.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'learnController': Unsatisfied dependency expressed through field 'learnService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'learnServiceImpl': Unsatisfied dependency expressed through field 'learnDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'learnDaoImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected single matching bean but found 2: primaryJdbcTemplate,secondaryJdbcTemplate
報(bào)錯(cuò)信息有幾個(gè),之前沒(méi)有添加多源數(shù)據(jù)的時(shí)候正常,主要是后面這個(gè)報(bào)錯(cuò):
主要問(wèn)題報(bào)錯(cuò)位置
(五)報(bào)錯(cuò)分析
從字面意思可以知道,本來(lái)只需要一個(gè)bean,但是找到了兩個(gè)bean,系統(tǒng)不知道需要用到哪一個(gè)bean,所以報(bào)錯(cuò),而系統(tǒng)所找到的就是我們之前配置的兩個(gè)多源數(shù)據(jù)寫(xiě)的bean。
回到剛才的配置類(lèi),我們發(fā)現(xiàn),我們已經(jīng)在第二數(shù)據(jù)源上面加上了@Primary,按道理來(lái)說(shuō),系統(tǒng)應(yīng)該默認(rèn)執(zhí)行該方法,不至于說(shuō)找不到需要選擇執(zhí)行哪個(gè)bean
等等.....
剛才我們添加的@Primary的方法只是用來(lái)讀取配置文件該選擇哪個(gè)數(shù)據(jù)庫(kù)而已,但是對(duì)JdbcTemplate支持的方法并沒(méi)有告訴系統(tǒng)該執(zhí)行哪一個(gè)!!!!!
那么問(wèn)題就很好解決了
真相只有一個(gè)......
在secondaryJdbcTemplate方法上添加@Primary注解,如下:
@Primary
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
ok ,重新運(yùn)行測(cè)試單元
久違的綠色又出來(lái)了
綠了綠了,哈哈哈,看一下數(shù)據(jù)庫(kù)是否添加成功
看一下兩個(gè)庫(kù)的數(shù)據(jù)是否添加成功
數(shù)據(jù)添加成功,兩個(gè)庫(kù)中都有數(shù)據(jù)
原文來(lái)源鏈接:
https://www.jianshu.com/p/c2ec8a283a80