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