java resource和_java – @Qualifier和@Resource之间的区别

@Autowired可以单独使用。如果单独使用,将按类型进行连接。因此,如果在容器中声明了同一类型的多个bean,则会出现问题,因为@Autowired不知道要使用哪个bean来注入。因此,将@Qualifier与@Autowired一起使用,以通过指定bean名称(按名称连线)来澄清实际连接的bean。

@Resource也通过名称连线。所以如果@Autowired与@Qualifier一起使用,它与@Resource相同。

区别是@Autowired和@Qualifier是弹簧注释,而@Resource是标准的java注释(来自JSR-250)。此外,@Resource只支持字段和setter注入,而@Autowired支持字段,setter,constructor和多参数方法注入。

建议使用@Resource进行字段和设置器注入。坚持使用@Qualifier和@Autowired作为构造函数或多参数方法注入。

If you intend to express annotation-driven injection by name, do not primarily use @Autowired – even if is technically capable of referring to a bean name through @Qualifier values. Instead, prefer the JSR-250 @Resource annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值