Spring boot @ConfigurationProperties not loaded

As the name says, I'm trying to use Configuration Configuration Properties to load a list of DataSourceConfig objects. I have a setter / getter lobster

Key Application Class Annotations

 @Slf4j @SpringBootApplication @EnableConfigurationProperties public class Application { 

Pojo configuration

 @Data public class DataSourceConfig { private String key; private String dbname; private String dbpath; } 

Yml file

 tenantdb: dataSourceConfig: - key: default dbpath: file:eventstore/jdbc/database dbname: defaultdb - key: other dbpath: file:eventstore/jdbc/other dbname: dslfjsdf 

Finally, the Spring Configuration class with @ConfigurationProperties annotation.

 @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; } 

With the above configuration, I get:

 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 

I tried various combinations. If I change the annotation to @ConfigurationProperties(prefix="tenantdb.dataSourceConfig") , I will not get an error, but List<DataSourceConfig> empty.

HELP !!

+5
source share
1 answer

You should use configuration properties as simple POJOs with getters and setters only and have a separate HsqlConfiguration that introduces these properties.

Something like that:

 @Component @ConfigurationProperties(prefix="tenantdb", locations={"datasources.yml"}) public class TenantDbProperties { //DataSourceConfig is POJO with key, dbpath and dbname private List<DataSourceConfig> dataSourceConfigs; public List<DataSourceConfig> getDataSourceConfigs(){ return dataSourceConfigs; } public void setDataSourceConfigs(List<DataSourceConfig> dataSourceConfigs){ this.dataSourceConfigs = dataSourceConfigs; } } 

And in a separate class these properties are introduced as:

 @Configuration @Profile("hsqldb") @ImportResource(value = { "persistence-config.xml" }) @Slf4j public class HsqlConfiguration { @Autowired private TenantDbProperties tenantDbProperties; //code goes here where you can use tenantDbProperties.getDataSourceConfigs() } 
+5
source

Source: https://habr.com/ru/post/1215652/


All Articles