Spring @Autowired and @Qualifier

Is it automatically detected with @Autowired? Is it dependency injection by name when using @Qualifier? How can we inject setter and constructor using these annotations?

+33
source share
3 answers

You can use @Qualifierwith @Autowired. In fact, Spring will ask you to explicitly select a component if an ambiguous type of component is detected, in which case you must provide a qualifier

.For example, in the following case, you must provide a qualifier

@Component
@Qualifier("staff") 
public Staff implements Person {}

@Component
@Qualifier("employee") 
public Manager implements Person {}


@Component
public Payroll {

    private Person person;

    @Autowired
    public Payroll(@Qualifier("employee") Person person){
          this.person = person;
    }

}

EDIT:

Lombok 1.18.4, , , @Qualifier, :

@Component
@Qualifier("staff") 
public Staff implements Person {}

@Component
@Qualifier("employee") 
public Manager implements Person {}


@Component
@RequiredArgsConstructor
public Payroll {
   @Qualifier("employee") private final Person person;
}

, lombok.config copyableAnnotations lombok ( lombok.config ):

# Copy the Qualifier annotation from the instance variables to the constructor
# see https://github.com/rzwitserloot/lombok/issues/745
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Qualifier

lombok 1.18.4.

, @Autowired @Qualifier , ( )

public Payroll {
   @Autowired @Qualifier("employee") private final Person person;
}

public Payroll {
   private final Person person;
   @Autowired
   @Qualifier("employee")
   public void setPerson(Person person) {
     this.person = person;
   } 
}

, , . , -

public Payroll {

    private Person person;

    @Autowired
    public Payroll(@Qualifier("employee") Person person){
          this.person = person;
    }

}
+44

@Qualifier , .

@Qualifier , @Component , @Bean. .

: -

public interface Vehicle {
     public void start();
     public void stop();
}

: Car Bike .

@Component(value="car")
public class Car implements Vehicle {

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

     @Override
     public void stop() {
           System.out.println("Car stopped");
     }
 }

@Component(value="bike")
public class Bike implements Vehicle {

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

     @Override
     public void stop() {
          System.out.println("Bike stopped");
     }
}

Bike Bean VehicleService @Autowired @Qualifier. @Qualifier, NoUniqueBeanDefinitionException.

@Component
public class VehicleService {

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

    public void service() {
         vehicle.start();
         vehicle.stop();
    }
}

: - @Qualifier

+23

@Autowiredfor automatic connection (or search) by type
@Qualifierfor automatic connection (or search) by name
Another alternative for @Qualifier-@Primary

@Component
@Qualifier("beanname")
public class A{}

public class B{

//Constructor
@Autowired  
public B(@Qualifier("beanname")A a){...} //  you need to add @autowire also 

//property
@Autowired
@Qualifier("beanname")
private A a;

}

//If you don't want to add the two annotations, we can use @Resource
public class B{

//property
@Resource(name="beanname")
private A a;

//Importing properties is very similar
@Value("${property.name}")  //@Value know how to interpret ${}
private String name;
}

more about @value

+8
source

Source: https://habr.com/ru/post/1662067/


All Articles