๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring @Qualifier Annotation


Introduction

  • The @Qualifier annotation in Spring is used to resolve ambiguity when multiple beans of the same type are available for dependency injection.
  • It works in conjunction with @Autowired to specify which bean to inject.

  • How @Qualifier Works ?
    • Bean Identification: It uses the bean name specified in the @Qualifier annotation to identify the correct bean.
    • Combines with @Autowired: It works only when used alongside @Autowired or similar annotations like @Inject.

  • Placement of @Qualifier :
    @Qualifier annotation annotation can be applied to Constructor Parameters, Setter Methods & Fields which are explained as below:

    1. Constructor Parameters:
      • Example:
        @Autowired
        public Car(@Qualifier("petrolEngine") Engine engine)
        {
            this.engine = engine;
        }

    2. Setter Injection:
      • Example:
        @Autowired
        @Qualifier("electricEngine")
        public void setEngine(Engine engine)
        {
            this.engine = engine;
        }

    3. Field Injection:
      • Example:
        @Autowired
        @Qualifier("dieselEngine")
        private Engine engine;

  • Program for @Qualifier
    We have created the below program for @Qualifier annotation which is used with setter method, try to create program in which @Qualifier is used in constructor parameter or with field.
    Engine.java
    package in.sp.beans;
    
    public class Engine 
    {
        private String type;
    
        public Engine(String type) 
        {
            this.type = type;
        }
    
        public void start() 
        {
            System.out.println(type + " engine started...");
        }
    }
    Car.java
    package in.sp.beans;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Car 
    {
        private Engine engine;
    
        // Setter method for dependency injection with @Qualifier
        @Autowired
        @Qualifier("dieselEngine")  // Specify the exact bean to inject
        public void setEngine(Engine engine) 
        {
            this.engine = engine;
        }
    
        public void drive() 
        {
            engine.start();
            System.out.println("Car is running...");
        }
    }
    AppConfig.java
    package in.sp.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    import in.sp.beans.Engine;
    
    @Configuration
    @ComponentScan(basePackages = "in.sp.beans")
    public class AppConfig
    {
        // Define multiple beans of the same type
    	
        @Bean(name = "petrolEngine")
        public Engine petrolEngine() 
        {
            return new Engine("Petrol");
        }
    
        @Bean(name = "dieselEngine")
        public Engine dieselEngine() 
        {
            return new Engine("Diesel");
        }
    }
    MainApp.java
    package in.sp.main;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import in.sp.beans.Car;
    
    public class MainApp 
    {
        public static void main(String[] args)
        {
            // Load Spring context from Java-based configuration
            ApplicationContext context = new AnnotationConfigApplicationContext(in.sp.config.AppConfig.class);
    
            // Retrieve the Car bean
            Car car = context.getBean(Car.class);
    
            // Call the drive method
            car.drive();
        }
    }
    Below is the output
    Output:
    Diesel engine started...
    Car is running...