mat*_*tus 56 configuration spring
我有以下配置类:
@Configuration
@PropertySource(name = "props", value = "classpath:/app-config.properties")
@ComponentScan("service")
public class AppConfig {
Run Code Online (Sandbox Code Playgroud)
我有财产服务:
@Component
public class SomeService {
@Value("#{props['some.property']}") private String someProperty;
Run Code Online (Sandbox Code Playgroud)
当我想要测试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'
Run Code Online (Sandbox Code Playgroud)
该问题记录在SPR-8539中
但无论如何我无法弄清楚如何配置PropertySourcesPlaceholderConfigurer 以使其工作.
这种方法适用于xml配置
<util:properties id="props" location="classpath:/app-config.properties" />
Run Code Online (Sandbox Code Playgroud)
但我想用java进行配置.
bay*_*ren 123
正如@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();
}
}
Run Code Online (Sandbox Code Playgroud)
laf*_*ste 30
如果使用@PropertySource,则必须使用以下内容检索属性:
@Autowired
Environment env;
// ...
String subject = env.getProperty("mail.subject");
Run Code Online (Sandbox Code Playgroud)
如果要使用@Value("$ {mail.subject}")进行检索,则必须通过xml注册prop占位符.
原因:https: //jira.springsource.org/browse/SPR-8539
Sac*_*ngh 16
我发现原因@value
不适合我,@value
需要PropertySourcesPlaceholderConfigurer
而不是一个PropertyPlaceholderConfigurer
.我做了相同的更改,它对我有用,我使用的是Spring 4.0.3版本.我在配置文件中使用下面的代码配置了它.
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)
cwa*_*ash 12
你的@Configuration类上是否需要一个方法来返回PropertySourcesPlaceholderConfigurer,带注释的@Bean并且是静态的,用Spring注册任何@PropertySource?
http://www.baeldung.com/2012/02/06/properties-with-spring/#java
https://jira.springsource.org/browse/SPR-8539
我遇到了同样的问题.@PropertySource
打得不好@Value
.一个快速的解决方法是使用XML配置,您可以@ImportResource
像往常一样从Spring Java Configuration中引用它,并且XML配置文件将包含一个条目:( <context:property-placeholder />
当然还有所需的命名空间仪式).没有任何其他更改@Value
将在您的@Configuration
pojo中注入属性.
这也可以用java这种方式配置
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setIgnoreResourceNotFound(true);
return configurer;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
87737 次 |
最近记录: |