我试图连接数据源以从应用程序(yml)文件中获取属性,但Datasourcebuilder没有读取这些属性。我提到了Stackoverflow以及Spring Boot docs,但是在我的代码中看不到任何东西。 我粘贴下面的代码是使用Spring引导1.4.3.RELEASESpring Boot数据源使用ConfigurationProperties进行了注释,但未加载属性
@SpringBootApplication
@EnableConfigurationProperties
@ComponentScan
public class MyApplication {
@Bean(name="dmDs")
@Primary
@ConfigurationProperties("spring.datasource")
public DataSource dmDataSource(){
return DataSourceBuilder.create().build();
}
@Bean
public String aBean(){
DataSource ds = dmDataSource(); // creates a datasource with URL, username and password empty.
return new String("");
}
如下图所示的应用程序配置文件:
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
- org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
profiles:
active: test
---
spring:
profiles: test
datasource:
url: jdbc:oracle:thin:SOME_URL
driver-class-name: oracle.jdbc.OracleDriver
password: test
username: test
datacollector:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@SOME_URL
username: user
password: pass
我看到在属性的读操作日志如下图所示application.yml文件
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.url' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.driver-class-name' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.password' in [applicationConfig: [classpath:/application.yml]] with type [String]
[main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.username' in [applicationConfig: [classpath:/application.yml]] with type [String]
JdbcTemplateAutoConfiguration matched:
- @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.core.JdbcTemplate' (OnClassCondition)
- @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) found a primary bean from beans 'cipDs', 'dmDs' (OnBeanCondition)
我运行的应用程序:
公共静态无效的主要(字串[] args){
SpringApplication.run(new Object[]{DecisionManagementApplication.class,ApplicationConfig.class}, args);
}
为什么?您正在使用默认属性来创建数据源。这已经由Spring Boot完成了。那么为什么你需要增加复杂性呢? –
我不想默认,但由于我的前缀不起作用,试图用这个来测试。我从日志中发现启动期望是“主要”。我甚至尝试添加(在我的问题更新),但同样的问题 – patb23
需要有一个'@ Primary'数据源,让它自动连接到相关的其他类(健康检查,'JdbcTemplate'等)。但是,如果你需要一个单一的数据源,就没有必要增加这种复杂性了....你怎么测试这个? –