tags:

views:

407

answers:

2

Is it possible to add a property from PropertyPlaceholder to a bean via @Autowired? I can't inject it in the xml-context-config because the beans are loaded this way:

<context:component-scan base-package="..."/>
+3  A: 

In spring 3.0 (i think from Milestone 3) you can use @Value(${foo.bar}) to access properties from PropertyPlaceholder.

semberal
can you give me an example for this? i am using spring 3 rc3 and when i try this way (e.g. @Value("${foo.bar}") ) then my attribute gets the value "foo.bar" ...
woezelmann
Yes, lets say you have property placeholder configured like this: `<context:property-placeholder location="classpath:app.properties"/>` you can inject the value of property with key `foo.bar` from `app.properties` to you bean: `class MyBean { @Value(${"foo.bar"} private String value; }`
semberal
Sorry for the formatting, I'm new in here :)
semberal
ok, now i got it :D thanks !
woezelmann
+3  A: 

A spring 2.5 approach:

@Component
public class Foo {
    @Autowired 
    @Qualifier("myFlag")
    private Boolean flag;
    /* ... */
}

and the context

<context:component-scan base-package="..."/>
<context:property-placeholder location="classpath:app.properties"/>
<!-- the flag bean -->
<bean id="myFlag" class="java.lang.Boolean">
    <constructor-arg value="${foo.bar}"/>
</bean>

Cheers

Paul McKenzie
+1 alternatively, and a bit less verbosely, `@Resource(name="myFlag")`
skaffman