java Spring boot @ConfigurationProperties 未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29140065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:44:49  来源:igfitidea点击:

Spring boot @ConfigurationProperties not loaded

javaspringspring-bootconfigurationproperty

提问by Raghu

As the title says, I'm trying to use Typesafe Configuration Propertiesto load a list of DataSourceConfigobjects. I have lombok for setter/getters

正如标题所说,我正在尝试使用类型安全配置属性来加载DataSourceConfig对象列表。我有用于 setter/getter 的 lombok

The main application class annotations

主要应用类注解

@Slf4j
@SpringBootApplication
@EnableConfigurationProperties
public class Application {

The configuration pojo

配置pojo

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

The yml file

.yml 文件

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.

最后,带有@ConfigurationProperties 注释的 Spring Configuration 类。

@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.

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

HELP!!

帮助!!

采纳答案by Nenad Bozic

You should use configuration properties as simple POJO with only getters and setters and have separate HsqlConfigurationwhich has this properties injected.

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

Something like this:

像这样的东西:

@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 separate class have this properties injected 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()
}