Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Autowired and PropertyPlaceholder

Tags:

java

spring

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="..."/>
like image 444
woezelmann Avatar asked Jan 19 '10 14:01

woezelmann


2 Answers

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

like image 144
semberal Avatar answered Nov 02 '22 23:11

semberal


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

like image 32
Paul McKenzie Avatar answered Nov 03 '22 00:11

Paul McKenzie