我有以下配置类:
@Configuration
@PropertySource(name = "props", value = "classpath:/app-config.properties")
@ComponentScan("service")
public class AppConfig {
我有财产服务:
@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'
该问题记录在 SPR-8539 中
但无论如何我无法弄清楚如何配置PropertySourcesPlaceholderConfigurer 以使其工作。
编辑 1
这种方法适用于 xml 配置
<util:properties id="props" location="classpath:/app-config.properties" />
但我想使用java进行配置。