views:

667

answers:

5

I have in my applicationContext.xml

<context:property-placeholder location="classpath*:*.properties" />


<bean id="clientPreferencesManager" class="pl.bildpresse.bildchat2.business.ClientPreferencesManager" >
    <property name="clientApiUrl" value="${clientapi.url}" />     
</bean>

Is it possible to do the same by autowire ? Something like :

@Autowired
@Qualifier("${clientapi.url}")
public void setClientApiUrl(String clientApiUrl) {
    this.clientApiUrl = clientApiUrl;
}
+4  A: 

You can use @Value:

@Value("${clientapi.url}") 
public void setClientApiUrl(String clientApiUrl) { 
    this.clientApiUrl = clientApiUrl; 
}
axtavt
true, for spring 3.0, which is the current version. (+1)
Bozho
+1  A: 

For spring 3.0, the correct way is the one shown - using @Value("${expression}")

For spring pre-3.0, you can try:

@Autowired
private StringValueResolver resolver;

There were no context initialization problems here, but I'm not sure it will work. Using the resolver you can resolve properties.

Bozho
A: 

My solution is to use

<context:property-override location="classpath:clientapi.properties" />

and then in clientapi.properties file

clientPreferencesManager.clientApiUrl=http://localhost:8084/ClientAPI/resources/

This one is good too

peperg
A: 

I've tried all the solutions proposed. None worked.

Costa
A: 

Ok. Just got it. You need to add @Autowired Something like:

@Autowired @Value("${clientapi.url}") private StringValueResolver resolver;

I'm using spring 3.0.0.RELEASE

Cheers

Costa