Does anyone know if I should be able to use property placeholder as an expression in a Qualifier? I can't seem to get this working.
I am using spring 3.0.4.
@Controller
public class MyController {
@Autowired
@Qualifier("${service.class}")
Service service;
}
@Service
@Qualifier("ServiceA")
ServiceA implements Service {
public void print() {
System.out.println("printing ServiceA.print()");
}
}
@Service
@Qualifier("ServiceB")
ServiceB implements Service {
public void print() {
System.out.println("printing ServiceB.print()");
}
}
XML:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:/etc/config.properties"/>
</bean>
config.properties:
config.properties
service.class=serviceB
This works. You can leave off the service names if you just use the default spring bean name. serviceA vs ServiceA, etc.
XML:
Props:
This solution works without XML and with properties file.
Yours classes improved:
MyController.java
:ServiceA.java
:ServiceB.java
:application.properties
(here you can change which class will be loaded):And important configuration file
AppConfig.java
:Additional explanations:
@Qualifier
only for field which will be autowired. For services, to specify bean name, use@Service
.@Service
with specyify name. For example, standard bean name for ServiceA isserviceA
(notServiceA
- see big first letter), so@Service("serviceA")
redundant (@Service
is enough).AppConfig
on this answer: Spring Bean Alias in JavaConfig.I would venture to guess the answer is no, just based on the write ups in a few javadoc pages. For example, see the docs for
@Value
:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Value.html
Notice they make special mention of using expressions in the annotation. For comparison, the docs for
@Qualifier
:http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/annotation/Qualifier.html
Which make no mention of expressions. Obviously not a definitive answer (but spring is generally very good on documentation). Also, if expressions were supported in the
@Qualifier
annotation I would expect they work the same way as the@Value
annotation (just based on spring being a very consistent framework).Spring 3.1 has the new profile bean feature, which seems like it can accomplish something like what you're trying to do. Here's a write up for that:
http://blog.springsource.com/2011/02/14/spring-3-1-m1-introducing-profile/
Maybe give this a whirl: