5
正如标题所示,我试图使用Typesafe Configuration Properties加载一个DataSourceConfig
对象的列表。我有龙目岛的制定者/吸气弹簧启动@ConfigurationProperties未加载
主要应用类注释
@Slf4j
@SpringBootApplication
@EnableConfigurationProperties
public class Application {
配置POJO
@Data
public class DataSourceConfig {
private String key;
private String dbname;
private String dbpath;
}
的YML文件
tenantdb:
dataSourceConfig:
-
key: default
dbpath: file:eventstore/jdbc/database
dbname: defaultdb
-
key: other
dbpath: file:eventstore/jdbc/other
dbname: dslfjsdf
最后,Spring配置类@ConfigurationProperties注释。
@Configuration
@Profile("hsqldb")
@ImportResource(value = { "persistence-config.xml" })
@Slf4j
@ConfigurationProperties(prefix="tenantdb", locations={"datasources.yml"})
public class HsqlConfiguration {
private List<DataSourceConfig> dataSourceConfig = new ArrayList<>();
@Bean
public List<DataSourceConfig> getDataSourceConfig() {
return dataSourceConfig;
}
通过配置上面,我得到:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hsqlConfiguration': Could not bind properties to [unknown] (target=tenantdb, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:303)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initia
我已经试过各种组合。如果我将注释更改为@ConfigurationProperties(prefix="tenantdb.dataSourceConfig")
,我不会收到错误,但List<DataSourceConfig>
为空。
帮助!
我的配置属性与注释'@ Component'并得到填补,而组件扫描,已你试过了吗?还有两件事,'datasources.yml'位于何处,为什么'getDataSourceConfig'注释为bean? – 2015-03-19 09:00:46
'datasources.yml'位于类路径根目录下。 'getDataSourceConfig'被注释为一个bean,这样我就可以在别处注入它了。 – Raghu 2015-03-19 09:13:53
我试图玩弄你的代码,创建测试,并按预期得到2'DataSourceConfig'的列表。唯一的问题是它们是空的(对'key','dbname'和'dbpath'有'null'。我在这个类中提供了setter,它绑定的很好,可能是这样吗? – 2015-03-19 09:46:34