日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢(xún)客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

配置流程

(一)配置文件配置

#多源數(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ò):

SpringBoot配置多數(shù)據(jù)源Consider marking one of the beans as @Primary

主要問(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è)試單元

SpringBoot配置多數(shù)據(jù)源Consider marking one of the beans as @Primary

久違的綠色又出來(lái)了

綠了綠了,哈哈哈,看一下數(shù)據(jù)庫(kù)是否添加成功

SpringBoot配置多數(shù)據(jù)源Consider marking one of the beans as @Primary

看一下兩個(gè)庫(kù)的數(shù)據(jù)是否添加成功


SpringBoot配置多數(shù)據(jù)源Consider marking one of the beans as @Primary

數(shù)據(jù)添加成功,兩個(gè)庫(kù)中都有數(shù)據(jù)


原文來(lái)源鏈接:
https://www.jianshu.com/p/c2ec8a283a80

分享到:
標(biāo)簽:SpringBoot
用戶(hù)無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定