DEV Community

realNameHidden
realNameHidden

Posted on

@Qualifier Example in Spring Boot

In Spring, when you have multiple beans of the same type, Spring gets confused about which one to inject. This is where @Qualifier comes in — it helps Spring choose the exact bean you want.

💬 Why is @Qualifier Needed?
Imagine this scenario:

public interface Vehicle {
    void start();
}

Enter fullscreen mode Exit fullscreen mode

Now you create two beans:

@Component
public class Car implements Vehicle {
    public void start() {
        System.out.println("Car starting...");
    }
}

@Component
public class Bike implements Vehicle {
    public void start() {
        System.out.println("Bike starting...");
    }
}

Enter fullscreen mode Exit fullscreen mode

Now if you try this:

@Autowired
private Vehicle vehicle;

Enter fullscreen mode Exit fullscreen mode

Spring throws an error: “No unique bean of type Vehicle” because it found 2 beans (Car and Bike). It doesn’t know which one to inject.

✅ Solution: Use @Qualifier

@Autowired
@Qualifier("bike")
private Vehicle vehicle;

Spring will now inject the Bike bean.

.

🛠 How It Works
@Qualifier matches the bean name.
The name can be:
Method name in @bean factory method
Class name (by default)
Name provided explicitly in @Component("customName")
💡 You Can Also Use It in Constructor

public MyService(@Qualifier("car") Vehicle vehicle) {
    this.vehicle = vehicle;
}

Enter fullscreen mode Exit fullscreen mode

Example

Image description

VechicleController

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.servic.VehicleService;

@RestController
public class VehicleController {

 private final VehicleService vehicleService;

  public VehicleController(VehicleService vehicleService) {
         this.vehicleService = vehicleService;
     }

  @GetMapping("/start")
     public String start() {
         vehicleService.startVehicle();
         return "Vehicle started successfully!";
     }
}

Enter fullscreen mode Exit fullscreen mode

VehicleService

package com.example.demo.servic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.example.demo.model.Vehicle;

@Service
public class VehicleService {


  Vehicle vehicel;

 @Autowired
 public VehicleService(@Qualifier("car") Vehicle vehicel) {
  this.vehicel = vehicel;
 }

  public void startVehicle() {
   vehicel.start();
     }
}

Enter fullscreen mode Exit fullscreen mode

Vehicle interface

package com.example.demo.model;

public interface Vehicle {
 void start();
}

Enter fullscreen mode Exit fullscreen mode

Car class

package com.example.demo.model;

import org.springframework.stereotype.Component;

@Component
public class Car implements Vehicle{

 @Override
 public void start() {
  System.out.println("Car is starting ");
 }

}

Enter fullscreen mode Exit fullscreen mode

Bike Class

package com.example.demo.model;

import org.springframework.stereotype.Component;

@Component
public class Bike implements Vehicle{

 @Override
 public void start() {
  System.out.println("Bike is starting ");
 }

}

Enter fullscreen mode Exit fullscreen mode

Dev Diairies image

User Feedback & The Pivot That Saved The Project ↪️

We’re following the journey of a dev team building on the Stellar Network as they go from hackathon idea to funded startup, testing their product in the real world and adapting as they go.

Watch full video 🎥

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay