当我的属性源是@Value
的子类时,我正在尝试使用PropertyPlaceholderConfigurer
在spring bean中设置字符串的值。有谁知道怎么做?
答案 0 :(得分:15)
老问题,但还是值得回答。您可以使用与原始PropertyPlaceholderConfigurer
相同的方式使用表达式。
app.properties
app.value=Injected
APP-context.xml中
<bean id="propertyConfigurer" class="MyPropertyPlaceholderConfigurer">
<property name="location">
<value>file:app.properties</value>
</property>
</bean>
目标bean中的
@Value(value="${app.value}")
private String injected;
使用Spring 3.0.6测试此方法
答案 1 :(得分:0)
您是否设法通过使用属性语法显式注入bean定义文件中的值来使其工作?从理论上讲,如果有效,那么你应该能够在@Value
中使用相同的表达式。就此而言,你应该能够使用@Autowired @Qualifier
答案 2 :(得分:0)
我认为在PropertyPlaceHolderConfigurer
注释中使用SPEL访问由@Value
加载的属性是不可能的。这将是伟大的,但据我所知,下一个最好的事情是声明:
<util:properties id="props" location="classpath:xxx/yyy/app.props"/>
它可以指向与PropertyPlaceHolderConfigurer
相同的属性文件。