简体   繁体   English

使用spring3 @Value访问PropertyPlaceholderConfigurer值?

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

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 . 当我的属性源是PropertyPlaceholderConfigurer的子类时,我正在尝试使用@Value在spring bean中设置字符串的值。 Anyone know how to do this ? 有人知道怎么做吗 ?

Old question, but still worth to be answered. 老问题,但还是值得回答。 You can use the expression the same way as you would with the original PropertyPlaceholderConfigurer . 您可以使用与原始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>

in the target bean 在目标bean中

    @Value(value="${app.value}")
    private String injected;

Tested this approach using Spring 3.0.6 使用Spring 3.0.6测试了这种方法

Have you managed to get it to work by explicitly injecting the value from the bean definition file using the property syntax? 您是否通过使用属性语法显式地从bean定义文件中注入值来设法让它工作? In theory, if that works, then you should be able to use the same expression in @Value . 理论上,如果有效,那么你应该能够在@Value使用相同的表达式。 For that matter, you should be able to do it using @Autowired @Qualifier also 就此而言,你应该能够使用@Autowired @Qualifier来做到这一点

I don't think it is possible to access properties loaded by PropertyPlaceHolderConfigurer using SPEL in a @Value annotation. 我不认为可以在@Value注释中使用SPEL访问PropertyPlaceHolderConfigurer加载的PropertyPlaceHolderConfigurer It would be great, but as far as I know, the next best thing is to declare: 这将是伟大的,但据我所知,下一个最好的事情是声明:

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

It can point to the same properties file as your PropertyPlaceHolderConfigurer . 它可以指向与PropertyPlaceHolderConfigurer相同的属性文件。

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

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