gpt4 book ai didi

spring - 使用 @PropertySource 注释时未解析 @Value。如何配置 PropertySourcesPlaceholderConfigurer?

转载 作者:IT老高 更新时间:2023-10-28 13:02:20 24 4
gpt4 key购买 nike

我有以下配置类:

@Configuration
@PropertySource(name = "props", value = "classpath:/app-config.properties")
@ComponentScan("service")
public class AppConfig {

我有属性(property)服务:

@Component 
public class SomeService {
@Value("#{props['some.property']}") private String someProperty;

当我想测试 AppConfig 配置类时收到错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String service.SomeService.someProperty; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'props' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' 

记录了该问题 in SPR-8539

但无论如何我不知道如何配置 PropertySourcesPlaceholderConfigurer 以使其工作。

编辑 1

这种方法适用于 xml 配置

<util:properties id="props" location="classpath:/app-config.properties" />

但我想使用java进行配置。

最佳答案

正如@cwash 所说;

@Configuration
@PropertySource("classpath:/test-config.properties")
public class TestConfig {

@Value("${name}")
public String name;


//You need this
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}

}

关于spring - 使用 @PropertySource 注释时未解析 @Value。如何配置 PropertySourcesPlaceholderConfigurer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13728000/

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