As the title says, I'm trying to use Typesafe Configuration Properties to load a list of DataSourceConfig objects. I have lombok for setter/getters
The main application class annotations
@Slf4j
@SpringBootApplication
@EnableConfigurationProperties
public class Application {
The configuration pojo
@Data
public class DataSourceConfig {
private String key;
private String dbname;
private String dbpath;
}
The 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 the @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 config above, 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've tried various combinations. If I change the annotation to @ConfigurationProperties(prefix="tenantdb.dataSourceConfig"), I don't get the error but List<DataSourceConfig> is empty.
HELP!!
@Componentand it gets filled while component scanning, have you tried that? Also two more things, where isdatasources.ymllocated and why isgetDataSourceConfigannotated as bean? - Nenad Bozicdatasources.ymlis at the classpath root.getDataSourceConfigis annotated as a bean so that I can inject it elsewhere as well. - RaghuDataSourceConfigas expected. Only thing is that they are empty (havenullforkey,dbnameanddbpath. I provided setters on that class and it binded fine, might be that? - Nenad Bozic@ConfigurationPropertiesbean is meant to be a simple pojo. All these annotations you added on it seems like the wrong place to me. Your@BeanonDataSourceConfigis definitely wrong. Please move only the configuration part to a bean with just@ConfigurationProperiteson it. Lombok is supported. - Stephane NicollTenantDbPropertieswhich are just@Componentand@ConfigurationProperties(prefix="tenantdb", locations={"datasources.yml"})and are simple pojo as @StéphaneNicoll suggested and haveHsqlConfigurationwith other configuration and component scan on package where properties are - Nenad Bozic