Spring启动多个数据源配置

时间:2017-06-20 10:53:08

标签: java spring spring-boot datasource jdbctemplate

我正在尝试在spring boot app中配置多个数据源。但是,当应用程序即将启动时,我会收到以下错误。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mySQLReplicationStatusService': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=msMysqlJdbcTemplate)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:47)
    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.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=msMysqlJdbcTemplate)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:47)
    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.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

这是我的配置类:

package com.example;
    @Configuration
    public class MultipleDBConfig {


        @Bean(name = "masterMysqlDb")
        @Primary
        @ConfigurationProperties(prefix = "spring.ds_master_mysql")
        public DataSource masterMysqlDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "msMysqlJdbcTemplate")
        public JdbcTemplate masterJdbcTemplate(@Qualifier("masterMysqlDb") DataSource masterMysqlDb) {
            return new JdbcTemplate(masterMysqlDb);
        }

        @Bean(name = "slaveMysqlDb")
        @ConfigurationProperties(prefix = "spring.ds_slave_mysql")
        public DataSource slaveMysqlDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "slaveMysqlJdbcTemplate")
        public JdbcTemplate slaveJdbcTemplate(@Qualifier("slaveMysqlDb") DataSource dsSlaveMySQL) {
            return new JdbcTemplate(dsSlaveMySQL);
        }
    }

这是我的服务类:

    package com.example.replication.service;
       @Service
public class MySQLReplicationStatusService {

    @Autowired
    @Qualifier("msMysqlJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("slaveMysqlJdbcTemplate")
    private JdbcTemplate slaveJdbcTemplate;

    public void checkReplicationStatus(String dbName, String tableName){
        System.out.println("In checkReplicationStatus");
        Connection conn_master = null;
        Statement stmt_master = null;
        StringBuilder sbSql_master = null;
        ResultSet rs_master = null;

        Connection conn_slave = null;
        Statement stmt_slave = null;
        StringBuilder sbSql_slave = null;
        ResultSet rs_slave = null;

        try{
            conn_master = jdbcTemplate.getDataSource().getConnection();
            stmt_master = conn_master.createStatement();
            sbSql_master = new StringBuilder();
            sbSql_master.append("SELECT COLUMN_NAME");
            sbSql_master.append("FROM information_schema.COLUMNS");
            sbSql_master.append("WHERE (TABLE_SCHEMA = " + dbName + ")");
            sbSql_master.append("AND (TABLE_NAME = " + tableName + ")");
            sbSql_master.append("AND (COLUMN_KEY = 'PRI') ");

            rs_master = stmt_master.executeQuery(sbSql_master.toString());

            /*conn_slave = slaveJdbcTemplate.getDataSource().getConnection();
            stmt_slave = conn_slave.createStatement();
            sbSql_slave = new StringBuilder();
            sbSql_slave.append("SELECT * FROM ").append(dbName).append(".").append(tableName);
            rs_slave = stmt_slave.executeQuery(sbSql_slave.toString());*/

            if(rs_master != null){
                while(!rs_master.isLast()){
                    rs_master.next();
                    Object obj = rs_master.getObject(1);
                    Object obj2 = rs_master.getObject(2);
                    String key = null;
                    String val = null;
                    if(obj != null){
                        key = obj.toString();
                        System.out.println("Key = " + key);
                    }
                    if(obj2 != null){
                        val = obj2.toString();
                        System.out.println("Value = " + val);
                    }
                }
            }
        }
        catch(SQLException e){
            e.printStackTrace();
        }
    }
}

这是组件类:

@Component
public class MySQLReplicationStatusCheck {
    final static Logger logger = Logger.getLogger(MySQLReplicationStatusCheck.class);

    @Autowired
    private MySQLReplicationStatusService mysqlReplicationStatusService = null;

    @Value("${master.db_name}")
    private  String databaseName;

    @Value("${master.table_name}")
    private  String tableName;

    public void replicationEvent(){
        logger.info("Inside replicationEvent method");
        System.out.println("Inside replicationEvent method");
        mysqlReplicationStatusService.checkReplicationStatus(databaseName, tableName);
    }
}

这是我的application.properties:

logging.config=./conf/log4j.xml

spring.ds_master_mysql.url = jdbc:mysql://<host>:3306
spring.ds_master_mysql.username = username
spring.ds_master_mysql.password = password
spring.ds_master_mysql.driverClassName=com.mysql.jdbc.Driver

 master.db_name=fdmsus_demo
 master.table_name=vitalstats

这是Spring启动类:

package com.example;
    @SpringBootApplication
    public class FdmsMysqlReplicationApplication {

        public static void main(String[] args) {
            SpringApplication.run(FdmsMysqlReplicationApplication.class, args);
        }
    }

然而,我无法弄清楚我在哪里错过了什么。任何人都可以帮我完成这件事。

感谢。

2 个答案:

答案 0 :(得分:1)

我认为问题在于在application.properties中声明驱动程序类名

logging.config=./conf/log4j.xml

spring.ds_master_mysql.url = jdbc:mysql://<host>:3306
spring.ds_master_mysql.username = username
spring.ds_master_mysql.password = password
spring.ds_master_mysql.driverClassName=com.mysql.jdbc.Driver

 master.db_name=fdmsus_demo
 master.table_name=vitalstats

使用以下application.properties

 logging.config=./conf/log4j.xml

    spring.ds_master_mysql.url = jdbc:mysql://<host>:3306
    spring.ds_master_mysql.username = username
    spring.ds_master_mysql.password = password
    spring.ds_master_mysql.driver-class-name=com.mysql.jdbc.Driver

     master.db_name=fdmsus_demo
     master.table_name=vitalstats

答案 1 :(得分:0)

SpringBoot配置多个数据源:

application.properties

#spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.generate-ddl=true
spring.jpa.database=default
logging.level.org.springframework = info
spring.jpa.show-sql=true
spring.jpa.open-in-view=true
spring.jpa.hibernate.ddl-auto=create-drop

# Connection properties of Data Source A
spring.datasource.a.url=jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC
spring.datasource.a.jdbcUrl=${spring.datasource.a.url}
spring.datasource.a.username = root
spring.datasource.a.password =
spring.datasource.a.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.a.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect


# Connection properties of Data Source B
spring.datasource.b.url=jdbc:mysql://localhost:3306/demo2?serverTimezone=UTC
spring.datasource.b.jdbcUrl=${spring.datasource.b.url}
spring.datasource.b.username = root
spring.datasource.b.password =
spring.datasource.b.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.b.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

ConfigA.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"api.a.repo"})
public class ConfigA {
    @Autowired
    private Environment env;

    @Primary
    @Bean(name = "dataSourceA")
    @ConfigurationProperties(prefix = "spring.datasource.a")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactoryA")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       @Qualifier("dataSourceA") DataSource dataSource) {

        Map<String, Object> properties = new HashMap<>();
        properties.put("hibernate.show-sql", env.getProperty("spring.datasource.a.jpa.show-sql"));
        properties.put("spring.jpa.hibernate.ddl-auto", env.getProperty("spring.datasource.a.hibernate.dialect"));
        properties.put("hibernate.dialect", env.getProperty("spring.datasource.a.hibernate.dialect"));

        return builder
            .dataSource(dataSource)
            .properties(properties)
            .packages("api.a.model")
            .persistenceUnit("datasourceA")
            .build();
    }

    @Primary
    @Bean(name = "transactionManagerA")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactoryA") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

ConfigB.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"api.b.repo"})
public class ConfigB {

    @Autowired
    private Environment env;

    @Bean(name = "dataSourceB")
    @ConfigurationProperties(prefix = "spring.datasource.b")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "entityManagerFactoryB")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
                                                                       @Qualifier("dataSourceB") DataSource dataSource) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("hibernate.show-sql", env.getProperty("spring.datasource.b.jpa.show-sql"));
        properties.put("spring.jpa.hibernate.ddl-auto", env.getProperty("spring.datasource.b.hibernate.dialect"));
        properties.put("hibernate.dialect", env.getProperty("spring.datasource.b.hibernate.dialect"));

        return builder
            .dataSource(dataSource)
            .packages("api.b.model")
            .persistenceUnit("datasourceB")
            .build();
    }


    @Bean(name = "transactionManagerB")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactoryB") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

访问:https://github.com/johanmoncada/OpenWork