Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Qualifier and property placeholder

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 
like image 829
Langali Avatar asked Oct 18 '11 19:10

Langali


People also ask

What is property placeholder in Spring?

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).

What is the difference between @autowired and @qualifier?

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.

What is the difference between @qualifier and @primary?

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.

What is the Java equivalent of Spring @autowired and @qualifier annotation?

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.


1 Answers

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 
like image 163
Matt Avatar answered Sep 21 '22 17:09

Matt