Tuesday, 18 August 2020

How @Qualifier and @Primary Annotation works in Spring

How @Qualifier and @Primary Annotation works in Spring


@Qualifier and @Primary Annotation both used in auto wiring of beans


@Primary Annotation

@Primary Annotation is used to mark a spring as  default primary bean for injection candidate if  container one more bean candidate for a injection bean location.

@Qualifier  Annotation

@Qualifier  Annotation allow to identify a particular bean when more the one bean implementation found for auto wiring during  resolution time of bean object injection  by spring container .

Following code example illustrate the working these annotation 
 
Employee.java

package in.jk.spring.autowired.test;

public interface Employee {

public String toString();

}

Developer.java

package in.jk.spring.autowired.test;

public class Developer implements Employee {

@Override
public String toString() {
return "Developer Bean";
}

}

DeveloperQualifier.java
package in.jk.spring.autowired.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface DeveloperQualifier {

}


Manager.java
package in.jk.spring.autowired.test;

public class Manager implements Employee {

@Override
public String toString() {
return "Manager Bean";
}

}

ManagerQualifier.java

package in.jk.spring.autowired.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface ManagerQualifier {

}

BeanConfiguration.java

package in.jk.spring.autowired.test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class SpringConfig {

@Bean
@Primary
@DeveloperQualifier()
public Employee getJavaEmp() {
return new Developer();

}

@Bean
@ManagerQualifier()
public Employee getManeger() {
return new Manager();

}

}



TestBean.java

package in.jk.spring.autowired.test;

import org.springframework.beans.factory.annotation.Autowired;

public class TestBean {

@Autowired
@DeveloperQualifier()
Employee developer;

@Autowired
@ManagerQualifier
Employee manager;

public void printEmpoyee() {

System.out.println(developer);
System.out.println(manager);

}

}

SpringApplication.java

package in.jk.spring.autowired.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {

public static void main(String[] args) {

ApplicationContext applicationContext = null;
applicationContext =new ClassPathXmlApplicationContext("context.xml");

SpringConfig configuration = (SpringConfig ) applicationContext.getBean("config");

TestBean testBean = (TestBean) applicationContext.getBean("testBean");

testBean.printEmpoyee();

}

}



context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config />
<context:component-scan
base-package="in.jk.spring.config" />
<context:property-placeholder
location="classpath:application.properties" />

<bean id="testBean" class="in.jk.spring.autowired.test.TestBean">

</bean>

<bean id="config"
class="in.jk.spring.autowired.test.SpringConfig ">

</bean>

</beans>  

No comments:

Post a Comment