×
☰ See All Chapters

Spring @Qualifier Annotation

@Qualifier annotation is applied on constructor arguments or method parameters and also used with @Autowired annotation to indicate which bean we want to use. This annotation is used as a qualifier for candidate beans when autowiring.  

Spring throws NoUniqueBeanDefinitionException when there are multiple beans resolved for same type. If Car is an interface and Audi, Ferrari are two different implementations of Car with both Audi and Ferrari annotated with @Component annotations, Spring throws NoUniqueBeanDefinitionException while creating application context. We have to two different beans resolved for Car type. Suppose if Car variable is used with @Autowired annotation, spring has a conflict for injecting the bean. But we need both Audi and Ferrari implementation of Car. Solution for this is using @Qualifier annotation.  After we use @Qualifier annotation with @Autowired annotation to indicate which bean we want to use, we use @Qualifier annotation by specifying a name or a qualifier when used with @Bean, @Service, @Component and @Repository annotation.

Now let us see an example for @Qualifier annotation.

Spring @Qualifier Annotation Example

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>Spring3_Qualifier</artifactId>

        <version>0.0.1-SNAPSHOT</version>

 

        <dependencies>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-core</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-context</artifactId>

                        <version>${spring.version}</version>

                </dependency>

                <dependency>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring-aspects</artifactId>

                        <version>${spring.version}</version>

                </dependency>

        </dependencies>

 

        <properties>

                <spring.version>4.2.4.RELEASE</spring.version>

        </properties>

 

</project>

Car.java

package com.java4coding.car;

 

public interface Car {

        public abstract void printCarName();

}

Audi.java

package com.java4coding.car;

 

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

import org.springframework.stereotype.Component;

 

@Component

@Qualifier("audi")

public class Audi implements Car{

 

        public void printCarName() {

                System.out.println("Audi");

        }

}

 

Ferrari.java

package com.java4coding.car;

package com.java4coding.car;

 

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

import org.springframework.stereotype.Component;

 

@Component

@Qualifier("ferrari")

public class Ferrari implements Car{

 

        public void printCarName() {

                System.out.println("Ferrari");

        }

}

CarService.java

package com.java4coding.car;

 

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

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

import org.springframework.stereotype.Service;

 

@Service

public class CarService{

 

        @Autowired

        @Qualifier("audi")

        Car car;

       

        public Car getCar() {

                return car;

        }

 

        public void setCar(Car car) {

                this.car = car;

        }

       

}

SpringQualifierAnnotationExample.java

package com.java4coding;

 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.java4coding.car.CarService;

 

@Configuration

@ComponentScan(basePackages = { "com.java4coding.car" })

public class SpringQualifierAnnotationExample {

       

        public static void main(String[] args) {

                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(

                                SpringQualifierAnnotationExample.class);

               

                CarService carService = (CarService)ctx.getBean("carService");

                carService.getCar().printCarName();

        }

}

 

Project Directory Structure

spring-qualifier-annotation-0
 

Output:

spring-qualifier-annotation-1
 
spring-qualifier-annotation-2
 

All Chapters
Author