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
The context:property-placeholder tag is used to externalize properties in a separate file. It automatically configures PropertyPlaceholderConfigurer , which replaces the ${} placeholders, which are resolved against a specified properties file (as a Spring resource location).
The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.
We use @Qualifier in Spring to autowire a specific bean among same type of beans, where as @Primary is used to give high preference to the specific bean among multiple beans of same type to inject to a bean.
The @Qualifier annotation is used to resolve the autowiring conflict, when there are multiple beans of same type. The @Qualifier annotation can be used on any class annotated with @Component or on methods annotated with @Bean . This annotation can also be applied on constructor arguments or method parameters.
This works. You can leave off the service names if you just use the default spring bean name. serviceA vs ServiceA, etc.
@Controller class MyController { @Autowired(required=false) @Qualifier("Service") Service service; public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("app-ctx.xml", MyController.class); for(String s:context.getBeanDefinitionNames()){ System.out.println(s); for(String t:context.getAliases(s)){ System.out.println("\t" + t); } } context.getBean(MyController.class).service.print(); } } public interface Service { void print(); } @Service(value="ServiceA") public class ServiceA implements example.Service { public void print() { System.out.println("printing ServiceA.print()"); } } @Service(value="ServiceB") public class ServiceB implements example.Service { public void print() { System.out.println("printing ServiceB.print()"); } }
XML:
<beans> <alias name="${service.class}" alias="Service"/> <context:property-placeholder location="example/app.properties"/> <context:component-scan base-package="example"/> <beans>
Props:
service.class=ServiceB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With