鸣谢:http://my.oschina.net/u/216467/blog/205951

@Resource和@Autowired两者都是做bean的注入使用。

其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入。但是Spring支持该注解的注入。

共同点:两者都可以写在字段和setter方法上。两者如果都写在字段上,就不需要写写setter方法。

不同点如下:

先来说一说@Autowired

@Autowired为Spring提供的注解,

需导入Package:org.springframework.beans.factory.annotation.Autowired;

只按照byType 注入。

@Autowired
private UserDao userDao;//用于字段上
@Autowired
public void setUserDao(UserDao userDao) {//用于属性的setter方法上
this.userDao= userDao;
}

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

@Autowired  @Qualifier("userDao")
private PersonDao personDao;

再说说@Resource

首先通过eclipe查看其官方解释:

javax.annotation.Resource

The Resource annotation marks a resource that is needed by the application.
This annotation may be applied to an application component class, or to fields
or methods of the component class. When the annotation is applied to a field or
method, the container will inject an instance of the requested resource into the
application component when the component is initialized. If the annotation is
applied to the component class, the annotation declares a resource that the
application will look up at runtime.

--资源注释标识应用程序所需要的资源。此注释可应用于应用组件类,或用于组件类的字段或方法。当将注释应用于某个字段或方法时,容器将在该组件初始化时向应用程序组件注入所需资源的实例。如果将注释应用于组件类,则该注释声明该应用程序将在运行时查找该资源。

@Resource默认按 byName 自动注入,由J2EE提供。

需导入Package:  javax.annotation.Resource

@Resource有两个中重要的属性:name和type ,而Spring将@Resource注解的name属性解析为bean的

名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用

type属性时则使用 byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Resource(name=“userDao”)
private UserDao userDao;//用于字段上
@Resource(name=“userDao”)
public void setUserDao(UserDao userDao) {//用于属性的setter方法上
this.userDao= userDao;
}

注:最好是将@Resource放在setter方法上。

@Resource装配顺序

  (1). 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常;

  (2). 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常;

  (3). 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常;

  (4). 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入。

Spring 注解 @Resource和@Autowired(转)的更多相关文章

  1. Spring注解@Resource和@Autowired区别对比

    转载:http://www.cnblogs.com/think-in-java/p/5474740.html @Resource和@Autowired都是做bean的注入时使用,其实@Resource ...

  2. Spring注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用.其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spri ...

  3. Spring 注解 @Resource和@Autowired

    @Resource和@Autowired两者都是做bean的注入使用. 其实@Resource并不是Spring的注解,他的包是javax.annotation.Resource 需要导入.但是Spr ...

  4. Spring注解@Resource和@Autowired的区别

    @Resource和@Autowired都是用来做bean的依赖注入的,两者都可以写在字段和setter方法上. java为我们提供了 javax.annotation.Resource这个注解. s ...

  5. Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象

    -------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...

  6. 【转载】Spring注解@Resource和@Autowired区别对比

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...

  7. 注解 @Resource与@Autowired与@Component的使用

    在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才 ...

  8. spring中@Resource和@Autowired理解

    一.@Resource的理解 @Resource在bean注入的时候使用,@Resource所属包其实不是spring,而是javax.annotation.Resource,只不过spring支持该 ...

  9. 注解@Resource和@Autowired区别对比

    @Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...

随机推荐

  1. Redis - 发布/订阅模式

    Redis 提供了一组命令可以让开发者实现 “发布/订阅” 模式.“发布/订阅” 可以实现进程间的消息传递,其原理是这样的: “发布/订阅” 模式中包含两种角色,分别是发布者和订阅者.订阅者可以订阅一 ...

  2. mysql 游标嵌套循环实例

    BEGIN #Routine body goes here... ####所有的2个小时之前生成的待支付订单更新为已过期 DECLARE tmp_id INT; DECLARE tmp_order_i ...

  3. DOS批处理命令-CMD命令

    CMD命令是重新开始一个命令解析器的实例.当然,他的功能并不止这么简单. Windows コマンド インタープリターの新しいインスタンスを開始します. 语法结构 CMD [/A | /U] [/Q] ...

  4. Web 服务器上的请求筛选被配置为拒绝该请求,因为查询字符串过长

    get请求方式中,传递的参数的值不能太多.是有长度限制的. http://test.jz.gaitu.cn:80/appMana/TemplateImgOutput.aspx?TemplateID=1 ...

  5. PHP学习笔记 - 进阶篇(5)

    PHP学习笔记 - 进阶篇(5) 正则表达式 什么叫正则表达式 正则表达式是对字符串进行操作的一种逻辑公式,就是用一些特定的字符组合成一个规则字符串,称之为正则匹配模式. $p = '/apple/' ...

  6. java新手笔记28 文件

    1.目录.文件 package com.yfs.javase; import java.io.File; public class FileDemo1 { /** * File 目录 文件 */ pu ...

  7. Javascript中的函数

    Javascript中的函数 1.什么是函数 函数是被命名的,独立的,完成特定功能的代码段.其可能给调用它的程序返回值,我们把这个代码段就称之为"函数". 被命名的:函数大部分都是 ...

  8. FreeMarker-Built-ins for strings

    http://freemarker.org/docs/ref_builtins_string.html Page Contents boolean cap_first capitalize chop_ ...

  9. JS自定义属性的设置与获取

    以前感觉用JQuery来设置自定义属性很方便,现在没有用JQuery,要用原生的JavaScript来操作自定义属性. Jquery操作自定义属性的方法,很简洁: $("#test" ...

  10. eclipse安装ermaster建模插件

    下载ermaster.jar 放到plugins重启eclipse即可