Spring @Autowired和@Qualifier

Dha*_*ake 34 java spring

是否使用@Autowired自动检测?使用@Qualifier时是按名称依赖注入吗?我们如何使用这些注释进行setter和构造函数注入?

kuh*_*yan 47

你可以@Qualifier一起使用@Autowired.事实上,如果找到不明确的bean类型,spring会要求你显式选择bean,在这种情况下你应该提供限定符

例如,在以下情况下,必须提供限定符

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:

在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;
}
Run Code Online (Sandbox Code Playgroud)

如果您使用新的lombok.config规则copyableAnnotations(通过将以下内容放在项目根目录中的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
Run Code Online (Sandbox Code Playgroud)

这是最近在lombok 1.18.4中引入的.


Ani*_*rgi 24

@Qualifier当存在多个相同类型的bean时,注释用于解决自动装配冲突.

所述@Qualifier注释可以与注释的任何类使用@Component或与所注解的方法@Bean.此批注也可以应用于构造函数参数或方法参数.

例如: -

public interface Vehicle {
     public void start();
     public void stop();
}
Run Code Online (Sandbox Code Playgroud)

有两个豆,汽车和自行车实现车辆接口

@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");
     }
}
Run Code Online (Sandbox Code Playgroud)

使用@Autowired@Qualifier注释在VehicleService中注入Bike bean .如果您没有使用@Qualifier,它将抛出NoUniqueBeanDefinitionException.

@Component
public class VehicleService {

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

    public void service() {
         vehicle.start();
         vehicle.stop();
    }
}
Run Code Online (Sandbox Code Playgroud)

参考: - @Qualifier注释示例


Pre*_*raj 8

@Autowired自动装配(或搜索)通过型
@Qualifier自动装配(或搜索)按姓名
的其它备选选项@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;

}
Run Code Online (Sandbox Code Playgroud)
//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;
}
Run Code Online (Sandbox Code Playgroud)

更多关于@value的信息