@Resource
JDK默认提供的注解,javax.annotation属于JSR-250规范的一部分,Spring通过CommonAnnotationBeanPostProcessor来处理该注解,在实现依赖注入的时候的匹配顺序是:
基于名称
基于类型
基于@Qualifier
@Inject
javax.inject属于JSR-330提供的注解,需要javax.inject依赖支持
javax.inject
javax.inject
1
Spring通过AutowiredAnnotationBeanPostProcessor来处理该注解,处理顺序是:br/>基于类型
基于@Qualifier
基于名称和@Named
@Autowired
Spring提供的注解,org.springframework.beans.factory.annotation通过AutowiredAnnotationBeanPostProcessor处理,处理的顺序同样是:br/>基于类型
基于@Qualifier
基于名称
@Qualifier
Spring提供的注解,org.springframework.beans.factory.annotation用于接口有多个实现类指定使用实现类时使用。常与@Autowired与@Inject注解搭配使用。
例:interface Payment接口有两个实现类 PrivatePayment与PublicPayment
我们在声明使用时可以通过指定使用PrivatePayment实现类:
使用@Resource方式实现
@Resource(name = "privatePaymentService")
private PaymentService paymentService;
使用@Autowired+@Qualifier方式实现
@Autowired
@Qualifier(value = "privatePaymentService")
private PaymentService paymentService;