In Java Spring, when does it make sense to use the @Qualifier
annotation vs the @Profile
annotation?
Can't you use essentially either one as they are both conditional autowirings? What are the benefits or costs of using one or the other?

- 2,429
- 2
- 28
- 32

- 569
- 1
- 9
- 21
3 Answers
@Qualifier:
There may be a situation when you create more than one bean of the same type and want to wire only one of them with a property, in such case you can use @Qualifier annotation along with @Autowired to remove the confusion by specifying which exact bean will be wired.
Suppose you have two spring component class Toyota.class and Bmw.class both implementing a interface Car.class
@Component
public class Toyota implements Car
@Component
public class Bmw implements Car
Now if you want to autowire car object like this:
@Autowired
private Car car;
there will be a confusion which spring bean to wire, toyota or the bmw? So spring container will raise an error. That's where @Qualifier comes to rescue by telling the container exactly which implementation of car is needed to wire. So redefining the codes like this:
@Component
@Qualifier("toyota")
public class Toyota implements Car
@Component
@Qualifier("bmw")
public class Bmw implements Car
@Autowired
@Qualifier("toyota")
private Car car;
Now during wiring, spring container knows exactly which one to wire, Toyota implementation in this case. So that's what @Qualifier does.
@Profile:
@Profile allows to register beans by condition. For example, register beans based on the application running in development, test, staging or production environment.
In our previous example, suppose, we want to use toyota implementation during development and bmw during test. So redefining the code below:
@Component
@Profile("test")
public class Toyota implements Car
@Component
@Profile("dev")
public class Bmw implements Car
@Autowired
private Car car;
In this case, we do not need to specify @Qualifier during wiring. Spring container will wire the correct implementation according to the runtime profile.

- 8,132
- 2
- 28
- 42
-
3using applicaiton.properties file, we can specify which profile to be used at run time, Example: spring.profiles.active=test – mathan Jul 11 '20 at 05:11
-
Or the command line. using -Dspring.profiles.active flat `java -jar -Dspring.profiles.active=prod application.jar`. See https://stackoverflow.com/questions/31038250/setting-active-profile-and-config-location-from-command-line-in-spring-boot – PatS Dec 14 '22 at 22:16
Spring @Profile provide a way to segregate parts of your application configuration and make it be available only in certain environments, the annotation can be applied at class level or method level.
like :
@Configuration
@Profile("dev")
public class CacheConfigDev
@Configuration
@Profile("alive")
public class CacheConfigAlive
The @Qualifier annotation stands for helping to disambiguate bean references on the scenario that Spring would otherwise not be able to do so.
@Component
@Qualifier("carBean")
public class Car implements Vehicle {
@Component
@Qualifier("bikeBean")
public class Bike implements Vehicle{
@Component
public class VehicleService {
@Autowired
@Qualifier("carBean")
private Vehicle vehicle;

- 1,944
- 22
- 22
There are certain scenarios where we need environment-specific beans definitions, so as a developer we need to design a mechanism to use one bean definitions in certain contexts only, here spring framework features can be leveraged instead we doing all these bit, we can use the @Profile
annotation available.
As per Spring documentation:
The
@Profile
annotation allows you to indicate that a component is eligible for registration when one
@Profile
: Can be used at both class or method level.
Some useful tips and tricks from the documentation:
If a
@Configuration
class is marked with@Profile
, all of the@Bean
methods and@Import
annotations associated with that class will be bypassed unless one or more of the specified profiles are active.If a
@Component
or@Configuration
class is marked with@Profile({"p1", "p2"})
, that class will not be registered/processed unless profiles 'p1' and/or 'p2' have been activated.If a given profile is prefixed with the NOT operator (!), the annotated element will be registered if the profile is not active. For example, given
@Profile({"p1", "!p2"})
, registration will occur if profilep1
is active or if profilep2
is not active.

- 3,554
- 1
- 20
- 31

- 21
- 3