繁体   English   中英

使用spring3 @Value访问PropertyPlaceholderConfigurer值?

[英]Using spring3 @Value to access PropertyPlaceholderConfigurer values?

当我的属性源是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加载的PropertyPlaceHolderConfigurer 这将是伟大的,但据我所知,下一个最好的事情是声明:

<util:properties id="props" location="classpath:xxx/yyy/app.props"/>

它可以指向与PropertyPlaceHolderConfigurer相同的属性文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM