@Resource与@Autowired用法区别
spring中,@Resource和@Autowired都是做bean的注入时使用。使用过程中,有时候@Resource 和 @Autowired可以替换使用;有时,则不可以。
下面,根据自己的学习,整理下这两个注解使用中的共同点和不同点,及用法上的不同。
共同点
@Resource和@Autowired都可以作为注入属性的修饰,在接口仅有单一实现类时,两个注解的修饰效果相同,可以互相替换,不影响使用。
不同点
- @Resource是Java自己的注解,@Resource有两个属性是比较重要的,分是name和type;Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
- @Autowired是spring的注解,是spring2.5版本引入的,Autowired只根据type进行注入,不会去匹配name。如果涉及到type无法辨别注入对象时,那需要依赖@Qualifier或@Primary注解一起来修饰。
我们创建一个简单的springboot项目demo,
定义一个接口Human.java,里面一个方法 runMarathon,
一个实现类Man.java
一个Controller类HumanController.java,里面注入Human接口的实现
附各Java类源码
package com.example.annotation.service;/*** service接口定义* @author Administrator*/
public interface Human {/*** 跑马拉松* @return*/String runMarathon();
}package com.example.annotation.service.impl;import com.example.annotation.service.Human;
import org.springframework.stereotype.Service;/*** service接口第一实现类* @author Administrator*/
@Service
public class Man implements Human {public String runMarathon() {return "A man run marathon";}
}package com.example.annotation.controller;import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.annotation.service.Human;/*** controller层实现类* @author Administrator*/
@RestController
@RequestMapping("/an")
public class HumanController {@Resourceprivate Human human;@RequestMapping("/run")public String runMarathon() {return human.runMarathon();}
}
至此,代码整理完成,启动springboot,浏览器地址栏输入http://localhost:8080/an/run
改动一:
将HumanController.java 类中的注解替换为@Autowired,再次启动,可以正常访问,与上图相同,这里不再贴访问结果图。
改动二:
再增加一个实现类Woman.java
package com.example.annotation.service.impl;import com.example.annotation.service.Human;
import org.springframework.stereotype.Service;/*** service接口第二实现类* @author Administrator*/
@Service
public class Woman implements Human {public String runMarathon() {return "An woman run marathon";}
}
HumanController.java 注解使用@Resource
@Resourceprivate Human human;
启动springboot,控制台会报错,报错信息太多,截取关键信息
2018-09-10 16:07:10.362 WARN 5592 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'humanController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.example.annotation.service.Human' available: expected single matching bean but found 2: man,woman
找关键信息 expected single matching bean but found 2: man,woman,被期望的单一结果被匹配到两个结果man和woman。
这里,我们需要借助@Resource注解的name属性或@Qualifier来确定一个合格的实现类
代码修改为
@Resource(name="woman")private Human human;
或
@Resource@Qualifier("woman")private Human human;
上面,我们指定了Human接口的实现类是Woman.java,启动springboot,访问 http://localhost:8080/an/run
改动三:
在改动二的基础上,将注解替换为@Autowired,启动报错
Description:Field human in com.example.annotation.controller.HumanController required a single bean, but 2 were found:- man: defined in file [D:\DEV_ENV\springbootws\annotation\target\classes\com\example\annotation\service\impl\Man.class]- woman: defined in file [D:\DEV_ENV\springbootws\annotation\target\classes\com\example\annotation\service\impl\Woman.class]Action:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
报错信息很明显,HumanController需要一个bean实现,但是找到了两个 man 和woman
解决方案:使用@Primary注解,在有多个实现bean时告诉spring首先@Primary修饰的那个;或者使用@Qualifier来标注需要注入的类。
@Qualifier修改方式与改动二的相同,依然是修改HumanController.java 中间注入的Human上面,这里不再复述
@Primary是修饰实现类的,告诉spring,如果有多个实现类时,优先注入被@Primary注解修饰的那个。这里,我们希望注入Man.java ,那么修改Man.java为
package com.example.annotation.service.impl;import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.example.annotation.service.Human;/*** service接口第一实现类* @author Administrator*/
@Service
@Primary
public class Man implements Human {public String runMarathon() {return "A man run marathon";}
}
启动springboot后,可以看到注入的已经是Man.java 了。
以上,个人学习总结,如有不当之处,欢迎留言批评斧正。谢谢
@Resource与@Autowired用法区别相关推荐
- spring 注解说明以及@Resource和@Autowired的区别
2019独角兽企业重金招聘Python工程师标准>>> 一.spring常见的注解有 @Component.@Repository.@Service.@Controller @Aut ...
- @Resource和@Autowired的区别
前言 @Resource和@Autowired都是做bean的注入时使用,@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入. 其中@Resourc ...
- @resource与@autowired的区别
@resource与@authorwired在本质上它们的作用是一样的,都是省去为一个对象变量写get,set方法,自动为这个对象注入实例化对象即注入依赖.而它们的注入的方式还是有所区别的 @reso ...
- Spring中@Resource与@Autowired、@Qualifier的用法与区别
Spring中@Resource与@Autowired.@Qualifier的用法与区别 1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法 ...
- Spring注解@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...
- 同一接口有多个实现类,怎么来注入一个指定的实现?@Resource、@Autowired、@Qualifier
如果一个接口有2个以上不同的实现类, 那么如何Autowire一个指定的实现 1:首先,UserService接口有两个实现类 UserService1和 UserService2 UserServi ...
- spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?
问题:spring中@Inject和@Autowired的区别?分别在什么条件下使用呢? 我在浏览SpringSource上的一些博客,在其他一个博客中,那个作者用了@Inject,但是我觉得他用@A ...
- mysql nvarchar用什么代替_mysql中char、varchar、nvarchar数据类型的用法区别
mysql中char.varchar.nvarchar数据类型的用法区别 mysql中char.varchar.nvarchar数据类型的用法区别: 说明: 1.char: 固定长度的非 Unicod ...
- Junit和Junit.Jupiter.api用法区别
Junit和Junit.Jupiter.api用法区别写在了文章的总结处,这里先简单的介绍一下Junit用法. Junit 5 = Junit Platform + Junit Jupiter + J ...
最新文章
- 皁新哪学计算机好,北京科技大学计算机基础模拟AB .doc
- Keil C51的库函数
- android 应用无法安装程序,朋友android设备无法安装我的Android应用程序,我甚至不能为我自己做...
- python解析器原理_Python程序运行原理图文解析
- python提取excel数据视频_Python-爬取b站的热门视频并导入Excel中
- android引入外部moudle,Android Studio3.2,调用其他Module作为依赖,出现的问题。
- 游戏用户被锁定后出现的错误提示
- c++ 中引用()的用法和应用实例
- 安装激活visio2013 professional版本
- GJB 8114编码标准检查软件产品简介
- Qt信号与槽实现方式
- 华为手机一直android,华为手机内存不够用?这5个文件夹常清理,可以腾出近10个G内存...
- 如何在vue项目中增加网页logo
- 哈希值(hashCode)
- 生命科学主要供应商排行榜
- 赵望野:前端工程师的困惑
- c语言统计n个正整数中每个数出现的次数,C语言怎么统计每个数出现的个数
- “(CRON) info (No MTA installed, discarding output)”
- 根据年份和月份来计算天数
- excel表格怎么调整行高和列宽_Excel 表格技巧—一键调整行高列宽的方法
热门文章
- python弹框倒计时自动关闭_Dialog中显示倒计时,到时自己主动关闭
- 关于FPGA的多功能引脚(multi-function pin)
- springboot配置i18n国际化
- 天呐,MIT的猎豹机器人可以在奔跑中跳跃过障碍
- 专题讲座5 组合数学 学习心得
- Wannafly挑战赛5之可编程拖拉机比赛
- 程序员练手的120多个小项目
- c++_狄泰总结01c语言升级与特异
- 云联惠认证时间_2018云联惠案情最新进展 检察机关正式批捕38名犯罪嫌疑人
- mysql中count()函数用法:count(1)和count(*)有什么区别?