使用spring3 @Value访问PropertyPlaceholderConfigurer值?

问题描述:

当我的属性源是PropertyPlaceholderConfigurer的子类时,我正在尝试使用@Value在spring bean中设置字符串的值.有人知道该怎么做吗?

I'm trying to set the value of a string in a spring bean using @Value, when my property source is a subclass of PropertyPlaceholderConfigurer. Anyone know how to do this ?

旧问题,但仍然值得回答.您可以使用与原始PropertyPlaceholderConfigurer相同的方式来使用该表达式.

Old question, but still worth to be answered. You can use the expression the same way as you would with the original PropertyPlaceholderConfigurer.

app.properties

app.properties

    app.value=Injected

app-context.xml

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测试了这种方法

Tested this approach using Spring 3.0.6