【发布时间】:2009-11-16 12:41:12
【问题描述】:
当我的属性源是PropertyPlaceholderConfigurer 的子类时,我正在尝试使用@Value 在spring bean 中设置字符串的值。有人知道怎么做吗?
【问题讨论】:
当我的属性源是PropertyPlaceholderConfigurer 的子类时,我正在尝试使用@Value 在spring bean 中设置字符串的值。有人知道怎么做吗?
【问题讨论】:
老问题,但仍然值得回答。您可以像使用原始 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 测试了这种方法
【讨论】:
您是否设法通过使用属性语法从 bean 定义文件中显式注入值来使其工作?理论上,如果可行,那么您应该能够在@Value 中使用相同的表达式。就此而言,您应该也可以使用@Autowired @Qualifier 来做到这一点
【讨论】:
我认为在@Value 注释中使用SPEL 访问PropertyPlaceHolderConfigurer 加载的属性是不可能的。这会很棒,但据我所知,下一个最好的事情是声明:
<util:properties id="props" location="classpath:xxx/yyy/app.props"/>
它可以指向与您的PropertyPlaceHolderConfigurer 相同的属性文件。
【讨论】: