Using spring3 @Value to access PropertyPlaceholderConfigurer values?

别等时光非礼了梦想. 提交于 2019-12-01 15:12:27

问题


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 ?


回答1:


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.value=Injected

app-context.xml

    <bean id="propertyConfigurer" class="MyPropertyPlaceholderConfigurer">
      <property name="location">
        <value>file:app.properties</value>
      </property>
    </bean>

in the target bean

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

Tested this approach using Spring 3.0.6




回答2:


Have you managed to get it to work by explicitly injecting the value from the bean definition file using the property syntax? In theory, if that works, then you should be able to use the same expression in @Value. For that matter, you should be able to do it using @Autowired @Qualifier also




回答3:


I don't think it is possible to access properties loaded by PropertyPlaceHolderConfigurer using SPEL in a @Value annotation. 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.



来源:https://stackoverflow.com/questions/1741968/using-spring3-value-to-access-propertyplaceholderconfigurer-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!