gpt4 book ai didi

java - Spring boot @ConfigurationProperties 未加载

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:46 26 4
gpt4 key购买 nike

正如标题所说,我正在尝试使用 Typesafe Configuration Properties加载 DataSourceConfig 的列表对象。我有用于 setter/getter 的 lombok

主要应用类注解

@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

最后,带有@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;
}

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

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>是空的。

帮助!!

最佳答案

您应该使用配置属性作为简单的 POJO,只有 getter 和 setter,并且有单独的 HsqlConfiguration 注入(inject)了这个属性。

像这样:

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

并且在单独的类中将此属性注入(inject)为:

@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()
}

关于java - Spring boot @ConfigurationProperties 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com