Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Qualifier and Profile annotations

Tags:

java

spring

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?

like image 346
Linkx_lair Avatar asked Mar 02 '17 15:03

Linkx_lair


2 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.

like image 51
Monzurul Shimul Avatar answered Oct 09 '22 18:10

Monzurul Shimul


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;
like image 1
Tiago Medici Avatar answered Oct 09 '22 18:10

Tiago Medici