未加载Spring引导@ConfigurationProperties

Rag*_*ghu 5 java spring configurationproperty spring-boot

正如标题所示,我正在尝试使用Typesafe配置属性来加载DataSourceConfig对象列表.我有用于制定者/吸气者的lombok

主要的应用程序类注释

@Slf4j
@SpringBootApplication
@EnableConfigurationProperties
public class Application {
Run Code Online (Sandbox Code Playgroud)

配置pojo

@Data
public class DataSourceConfig {
    private String key;
    private String dbname;
    private String dbpath;
}
Run Code Online (Sandbox Code Playgroud)

yml文件

tenantdb:
    dataSourceConfig:
        -
            key: default 
            dbpath: file:eventstore/jdbc/database
            dbname: defaultdb
        -
            key: other
            dbpath: file:eventstore/jdbc/other
            dbname: dslfjsdf
Run Code Online (Sandbox Code Playgroud)

最后,Spring Configuration类带有@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;
    }
Run Code Online (Sandbox Code Playgroud)

通过上面的配置,我得到:

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
Run Code Online (Sandbox Code Playgroud)

我尝试了各种组合.如果我将注释更改为@ConfigurationProperties(prefix="tenantdb.dataSourceConfig"),我不会得到错误但是List<DataSourceConfig>为空.

救命!!

Nen*_*zic 5

您应该将配置属性用作仅具有getter和setter的简单POJO,并且具有HsqlConfiguration已注入此属性的单独属性.

像这样的东西:

@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;
  }
}
Run Code Online (Sandbox Code Playgroud)

在单独的类中,这些属性注入:

@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()
}
Run Code Online (Sandbox Code Playgroud)