【发布时间】:2016-11-21 20:37:48
【问题描述】:
我收到以下错误:
***************************
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") 唯一标识了数据源名称,然后将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;
}
}
第二个数据源类
@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 以黄色突出显示我的数据源,当将鼠标悬停在它们上面时,它显示为 "Cannot resolve configuration property"
--------------更新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."
},
并将其放置在资源文件夹中。
【问题讨论】:
-
用一个数据库试试可以吗?
-
@NikolayRusev 当我使用
spring.database.uri:类型约定时,是的,它对一个数据源工作得很好。但是我需要两个数据源,现在我遇到了这个问题。 -
@NikolayRusev 请看更新一
-
@NikolayRusev 请参阅更新 2
标签: spring-boot spring-boot-actuator