Spring Boot @Qualifier annotation

In this tutorial, we present the Spring Boot @Qualifier annotation. The @Qualifier annotation is used to differentiate beans of the same type. It can also be used to annotate other custom annotations that can then be used as qualifiers.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications with minimal effort.

The following three applications are command line Spring Boot applications.

Differentiating Person beans

In our application, we have two beans of Person type: Student and Manager. We use the @Qualifier annotation to distinguish between them.

$ tree
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── zetcode
    │               ├── Application.java
    │               ├── bean
    │               │   ├── Manager.java
    │               │   ├── Person.java
    │               │   └── Student.java
    │               └── MyRunner.java
    └── test
        └── java

This is the project structure of the Spring Boot application.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootQualifier</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <parent>
        <groupId>org.sringframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>    
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>        
       
    </dependencies>    

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>            
        </plugins>
    </build>         
    
</project>

This is the Maven build file. The spring-boot-starter is the core starter that includes auto-configuration support, logging, and YAML. The application is packaged into a JAR file.

Person.java
package com.zetcode.bean;

public interface Person {
    
    public String info();
}

We have an interface that defines the Person type.

Student.java
package com.zetcode.bean;

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

@Component
@Qualifier("student")
public class Student implements Person {

    @Override
    public String info() {
        
        return "Student";
    }
}

Student inherits from Person. @Component is a basic Spring annotation that allows Student to be detected by Spring containter. The @Qualifier("student") uniquely identifies this bean with the "student" string.

Manager.java
package com.zetcode.bean;

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

@Component
@Qualifier("manager")
public class Manager implements Person {

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

We have another bean called Manager. This bean is also identified with the @Qualifier("manager") annotation.

MyRunner.java
package com.zetcode;

import com.zetcode.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyRunner implements CommandLineRunner {
    
    @Autowired
    @Qualifier("student")
    Person p1;
    
    @Autowired
    @Qualifier("manager")
    Person p2;    
    
    @Override
    public void run(String... args) throws Exception {

        System.out.println(p1.info());        
        System.out.println(p2.info());
    }
}

The CommandLineRunner interface indicates that a bean should run when it is contained within a SpringApplication. It can be used to create command line applications in Spring Boot.

@Component
public class MyRunner implements CommandLineRunner {

The CommandLineRunner is also a Spring bean and is decorated with the @Component annotation; it is auto-detected by Spring.

@Autowired
@Qualifier("student")
Person p1;

We inject a Person bean into the p1 field. The @Qualifier("student") specifies that it is a Student bean.

@Autowired
@Qualifier("manager")
Person p2;   

Likewise, we inject the Manager bean into the p2 field.

Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

The Application sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

Using factory to create beans

In the second application, we use a factory class to generate beans. The pom.xml, Person.java, Application.java, MyRunner.java remain unchanged.

$ tree
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           ├── bean
    │   │           │   ├── Manager.java
    │   │           │   ├── Person.java
    │   │           │   └── Student.java
    │   │           ├── conf
    │   │           │   └── PersonFactory.java
    │   │           └── MyRunner.java
    │   └── resources
    └── test
        └── java

This is the project structure.

Manager.java
package com.zetcode.bean;

public class Manager implements Person {

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

The annotations are removed from the Manager class.

Student.java
package com.zetcode.bean;

public class Student implements Person {

    @Override
    public String info() {
        
        return "Student";
    }
}

Likewise, there are no annotations for the Student class.

PersonFactory.java
package com.zetcode.conf;

import com.zetcode.bean.Manager;
import com.zetcode.bean.Person;
import com.zetcode.bean.Student;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonFactory {

    @Bean
    @Qualifier("student")
    public Person createStudent() {
        return new Student();
    }
    
    @Bean
    @Qualifier("manager")
    public Person createManager() {
        return new Manager();
    }    
}

In the previous example, the beans were auto-detected by Spring. Here, the PersonFactory creates two beans with the help of the @Bean annotation.

@Bean
@Qualifier("student")
public Person createStudent() {
    return new Student();
}

The @Bean annotation marks methods that define beans. The @Qualifier("student") tells which implementation of the Person to create.

Creating custom @Qualifier annotation

To reduce code, we can create custom @Qualifier annotations.

tree
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           ├── bean
    │   │           │   ├── Manager.java
    │   │           │   ├── Person.java
    │   │           │   └── Student.java
    │   │           ├── conf
    │   │           │   └── PersonFactory.java
    │   │           ├── MyRunner.java
    │   │           └── qualifier
    │   │               └── PersonQ.java
    │   └── resources
    └── test
        └── java

This is the project structure; we list all files except for pom.xml, which is listed in the first application.

Person.java
package com.zetcode.bean;

public interface Person {
    
    public String info();
}

This is the Person type.

Manager.java
package com.zetcode.bean;

import org.springframework.stereotype.Component;

@Component
public class Manager implements Person {

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

The Manager class is decorated with @Component annotation; it will be auto-detected by Spring.

Student.java
package com.zetcode.bean;

import org.springframework.stereotype.Component;

@Component
public class Student implements Person {

    @Override
    public String info() {
        
        return "Student";
    }
}

The same applies for the Student bean.

PersonQ.java
package com.zetcode.qualifier;

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.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface PersonQ {

    String value();
}

Here we define a new @PersonQ qualifier.

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})

The @Targer annotation tells where the annotation can be applied. In our case, it can be applied to fields, methods, and parameters.

@Retention(RetentionPolicy.RUNTIME)

The @Retention annotation specifies how the marked annotation is stored. With RetentionPolicy.RUNTIME the marked annotation is retained by the JVM so it can be used by the runtime environment.

public @interface PersonQ {

The @interface keyword is used to declare a new annotation type.

PersonFactory.java
package com.zetcode.conf;

import com.zetcode.bean.Manager;
import com.zetcode.bean.Person;
import com.zetcode.bean.Student;
import com.zetcode.qualifier.PersonQ;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonFactory {

    @PersonQ("student")
    public Person createStudent() {
        return new Student();
    }

    @PersonQ("manager")
    public Person createManager() {
        return new Manager();
    }
}

In the PersonFactory we use the @PersonQ to identify what kind of beans are created.

MyRunner.java
package com.zetcode;

import com.zetcode.bean.Person;
import com.zetcode.qualifier.PersonQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyRunner implements CommandLineRunner {

    @Autowired
    @PersonQ("student")
    Person p1;

    @Autowired
    @PersonQ("manager")
    Person p2;

    @Override
    public void run(String... args) throws Exception {

        System.out.println(p1.info());
        System.out.println(p2.info());
    }
}

In the MyRunner, we inject beans with @Autowired and @PersonQ annotations.

Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

In Application, we set up the Spring Boot application.

In this tutorial, we have used the Spring Boot @Qualifier annotation. You might also be interested in the related tutorials: Serving static content in Spring Boot, Spring Boot DataSourceBuilder tutorial, Spring Boot iText tutorial, Spring Boot RESTFul application, Introduction to Spring web applications, Standalone Spring applications, Java tutorial.