Wiring in Spring: @Autowired, @Resource and @Inject 区别

refer:https://www.baeldung.com/spring-annotations-resource-inject-autowire

主要是查找顺序不一致:

@Resource

  1. Match by Name
  2. Match by Type
  3. Match by Qualifier

优先匹配Name @Resource(name="xx")

若没有传入名字则(即直接@Resource)匹配类型,

若一个类型有两个Bean,则还需匹配Qualifier(@Bean 时的类名或方法名),不然会报错

@Inject 

  1. Match by Type
  2. Match by Qualifier(多个子类实现时)
  3. Match by Name

匹配顺序如上

@Autowired

  1. Match by Type
  2. Match by Qualifier
  3. Match by Name

和@Inject 一样,

@Resource @Inject是JSR 的注解, 而@Autowire是Spring的注解

原文地址:https://www.cnblogs.com/daxiong225/p/13182213.html