声明Spring Bean和注入Bean的几种常用注解和区别

Spring 声明Bean的注解:

 

  • @Component: 组件,没有明确的角色。 
  • @Service : 在业务逻辑层(Service层)使用。
  • @Repository:  再数据访问层(Dao层)使用。
  • @Controller: 再展现层(MVC->Spring MVC)使用。
 
Spring 注入Bean的注解:
  • @Autowired:Spring提供的注解。
  • @inject:JSR-330提供的注解。
  • @Resource:JSP-250提供的注解。
 
‘@Autowired’ 和‘@Inject’他们都是通过‘AutowiredAnnotationBeanPostProcessor’ 类实现的依赖注入,二者具有可互换性。 
‘@Resource’通过 ‘CommonAnnotationBeanPostProcessor’ 类实现依赖注入,即便如此他们在依赖注入时的表现还是极为相近的。
以下是他们在实现依赖注入时执行顺序的概括:

@Autowired and @Inject
  1. Matches by Type
  2. Restricts by Qualifiers
  3. Matches by Name
@Resource
  1. Matches by Name
  2. Matches by Type
  3. Restricts by Qualifiers (ignored if match is found by name)
参考:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值