@Resource和@Autowired的区别
前言
@Resource和@Autowired都是做bean的注入时使用,@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。
其中@Resource并不是Spring的注解,它的包是javax.annotation.Resource,但是Spring支持该注解的注入。
1、共同点
1) @Resource和@Autowired都是用于bean的注入;
2)@Resource和@Autowired都可以标注在字段和setter方法上,两者如果都写在字段上,那么就不需要再写setter方法。
2、不同点
1)@Autowired 源码
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {boolean required() default true;
}
@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。
@Autowired注解是按照类型(byType)注入对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来注入,可以结合@Qualifier注解一起使用。
public class TestServiceImpl {@Autowired@Qualifier("testDao")private TestDao testDao;
}
2)@Resource 源码
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
@Repeatable(Resources.class)
public @interface Resource {/*** The JNDI name of the resource. For field annotations,* the default is the field name. For method annotations,* the default is the JavaBeans property name corresponding* to the method. For class annotations, there is no default* and this must be specified.*/String name() default "";String lookup() default "";Class<?> type() default java.lang.Object.class;enum AuthenticationType {CONTAINER,APPLICATION}AuthenticationType authenticationType() default AuthenticationType.CONTAINER;boolean shareable() default true;String mappedName() default "";String description() default "";
}
@Resource默认按照 byName 自动注入,由J2EE提供,需要导入包javax.annotation.Resource。
@Resource有两个重要的属性:name和type。
Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,便会通过反射机制使用 byName 自动注入策略。
@Resource 最好放在setter方法上,因为这样更符合面向对象的思想,通过set、get去操作属性,而不是直接去操作属性。
@Resource装配顺序也有不同:
①如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。
②如果指定了name,则从上下文中查找名称匹配的bean进行装配,找不到则抛出异常。
③如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或是找到多个,都会抛出异常。
④如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配。
补充
1)Spring 自动注入 autowire 中 byName 和 byType 的区别
Autowire modes
no |
默认不开启 |
byName |
根据被注入属性的名称作为 Bean 名称作为依赖查找,并将对象设置到该属性 |
byType |
根据被注入属性的类型作为依赖类型进行依赖查找,并将该对象设置到该属性 |
constructor |
特殊 byType 类型,用于构造器参数 |
@Resource和@Autowired的区别相关推荐
- spring 注解说明以及@Resource和@Autowired的区别
2019独角兽企业重金招聘Python工程师标准>>> 一.spring常见的注解有 @Component.@Repository.@Service.@Controller @Aut ...
- @Resource与@Autowired用法区别
spring中,@Resource和@Autowired都是做bean的注入时使用.使用过程中,有时候@Resource 和 @Autowired可以替换使用:有时,则不可以. 下面,根据自己的学习, ...
- @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 ...
- Spring 注解 @Resource和@Autowired(转)
鸣谢:http://my.oschina.net/u/216467/blog/205951 @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spri ...
- Spring 注解 @Resource和@Autowired
@Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...
最新文章
- Android之EventBus框架源码解析上(单例模式)
- pycharm没有python interpreter_首次安装Pycharm出现No Python interpreter selected解决方法
- 域 和 Session 的问题
- java 置顶_[置顶]java开发之基础篇2
- linux下阻塞的系统调用,Linux下文件的阻塞与非阻塞对部分系统调用的影响
- MYSQL正式环境主从复制(不锁表,不停服务)
- unity3d人物跳_Unity3D研究院之角色控制器组件研究(二十二)
- 14.深入分布式缓存:从原理到实践 --- 典型电商应用与缓存
- antd树形控件拖拽 改变数据位置
- Java入门 - 循环结构基础
- 简谈变电站综合自动化系统在变电站的应用分析
- CSS加粗知识与案例
- ST32位最小系统微控制器STM32F401介绍
- web服务器集群-------Apache网页优化 (1)网页压缩
- 如果讲不明白Spring Cloud核心组件,那我就白编故事了
- word学习-软回车替换为硬回车
- 【线性代数(2)】n阶行列式三种定义
- python读取pcd文件_(一)读取PCD文件
- 校内题目T2691 桶哥的问题——送桶
- C++:实现将华氏度转换为摄氏度