无法使用多个dataSource创建数据库连接

时间:2016-11-21 20:37:48

标签: spring-boot spring-boot-actuator

我收到以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource in com.domain.ws.actuator.RandomActuator required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

我使用@ConfigurationProperties(prefix="spring.fooDataSource")唯一标识了dataSources名称,然后将spring.fooDataSource.url: ${dbURI}放在yml文件中。例如:

配置类

@Configuration
@PropertySource({"classpath:application.yml"})
public class RequestDataSource {

    /*******************************
     *      foo Datasource     *
     * *****************************/
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.fooDataSource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /*******************************
     * foo Transaction manager *
     * *****************************/
    @Bean(name="tm1")
    @Primary
    @Autowired
    DataSourceTransactionManager tm1(@Qualifier("primaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }

}

第二个dataSource类

@Configuration
@PropertySource({"classpath:application.yml"})
public class KeyviewDataSource {

    /*******************************
     *     Second Datasource      *
     * *****************************/
    @Bean
    @ConfigurationProperties(prefix="spring.secondDataSource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /*******************************
     * Second Transaction manager *
     * *****************************/
    @Bean(name="tm2")
    @Autowired
    DataSourceTransactionManager tm2(@Qualifier("secondaryDataSource") DataSource datasource) {
        DataSourceTransactionManager txm  = new DataSourceTransactionManager(datasource);
        return txm;
    }

}

Application.yml

spring:

  fooDataSource.url: ${dbURI}
  fooDataSource.username: ${dbUsername}
  fooDataSource.password: ${dbPassword}
  fooDataSource.driverClassName: oracle.jdbc.OracleDriver

  secondDataSource.url: ${dbURIkeyview}
  secondDataSource.username: ${dbUSERNAMEkeyview}
  secondDataSource.password: ${dbPasswordKeyview}
  secondDataSource.driverClassName: oracle.jdbc.OracleDriver

我正在使用gradle,在这个模块中,我确实添加了它所需的依赖项:

compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: springBootVersion
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-java8
    compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.0.0.CR1'

----------------------------------更新1 ----------- -----------------------

注意

在我的applications.yml文件中,Intellij以黄色突出显示我的dataSources,当鼠标悬停在它们上面时,它会显示"Cannot resolve configuration property"

------------------------- Update 2 -------------------- ----------

由于进行了如此多的更改,我创建了一个与此HERE

相关的新问题

-------------------------------更新3 -------------- ----------------

我通过创建spring-configuration-metadata.json文件解决了更新问题  并执行以下操作:

{
    "properties": [
        {
            "name": "spring.fooDataSource.username",
            "type": "java.lang.String",
            "description": "Description for spring.requestDataSource.username."
        },
        {
            "name": "spring.fooDataSource.url",
            "type": "java.lang.String",
            "description": "Description for spring.requestDataSource.url."
        },
        {
            "name": "spring.fooDataSource.driverClassName",
            "type": "java.lang.String",
            "description": "Description for spring.requestDataSource.driverClassName."
        },

并将其放在资源文件夹中。

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的yml文件中。您可以在spring boot中轻松设置两个数据库。只需将文件Application.yml更改为:

即可
spring:
  fooDataSource.url: ${dbURI}
  fooDataSource.username: ${dbUsername}
  fooDataSource.password: ${dbPassword}
  fooDataSource.driverClassName: oracle.jdbc.OracleDriver

second:
  secondDataSource.url: ${dbURIkeyview}
  secondDataSource.username: ${dbUSERNAMEkeyview}
  secondDataSource.password: ${dbPasswordKeyview}
  secondDataSource.driverClassName: oracle.jdbc.OracleDriver

然后是你的配置类:

@Configuration
@PropertySource({"classpath:application.yml"})
public class RequestDataSource {

    /*******************************
     *      foo Datasource     *
     * *****************************/
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.fooDataSource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /*******************************
    *     Second Datasource      *
   * *****************************/
   @Bean
   @ConfigurationProperties(prefix="second.secondDataSource")
   public DataSource secondaryDataSource() {
      return DataSourceBuilder.create().build();
   }

}

因为我认为两个数据库不能以相同的前缀为前缀。