使用spring3 @Value访问PropertyPlaceholderConfigurer值?

时间:2009-11-16 12:41:13

标签: spring spring-el

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

3 个答案:

答案 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相同的属性文件。