@Resource @Autowired @Inject
‘@Autowired’ 和‘@Inject’的报错信息完全相同,他们都是通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入,二者具有可互换性。 ‘@Resource’通过 ‘CommonAnnotationBeanPostProcessor’ 类实现依赖注入,即便如此他们在依赖注入时的表现还是极为相近的,以下是他们在实现依赖注入时执行顺序的概括:
@Autowired and @Inject
- Matches by Type
- Restricts by Qualifiers
- Matches by Name
@Resource
- Matches by Name
- Matches by Type
- Restricts by Qualifiers (ignored if match is found by name)