krosenvold
krosenvold

Reputation: 77121

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. Anyone know how to do this ?

Upvotes: 11

Views: 11608

Answers (3)

pakman
pakman

Reputation: 1735

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.

Upvotes: 0

micfra
micfra

Reputation: 2820

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

Upvotes: 15

skaffman
skaffman

Reputation: 403451

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

Upvotes: 0

Related Questions