@Resource注解的官方解释
一、@Resource注解的官方解释
@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.
翻译:@Resource,它在语义上被定义为通过其唯一的名称来标识特定的目标组件,其中声明的类型与匹配过程无关。
If no name is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name.
翻译:如果没有明确指定名称,则默认名称是从字段名称或设置方法(get、set方法)派生的。 如果用在字段上,则采用字段名称; 如果用在在setter方法,它采用其属性名称(例如getProperty()方法,取property做为属性名称)。
Thus in the following example, the customerPreferenceDao field first looks for a bean named customerPreferenceDao, then falls back to a primary type match for the type CustomerPreferenceDao. The “context” field is injected based on the known resolvable dependency type ApplicationContext.
解释:customerPreferenceDao字段首先查找名字为customerPreferenceDao的bean,如果没找到该类,则以CustomerPreferenceDao类型进行匹配。
public class MovieRecommender {
@Resource
private CustomerPreferenceDao customerPreferenceDao;
@Resource
private ApplicationContext context;
public MovieRecommender() {
}
...
}
1
2
3
4
5
6
7
8
9
10
二、比较@Resource、@Autowired
@Resource
@Resource默认按byName自动注入。
既不指定name属性,也不指定type属性,则自动按byName方式进行查找。如果没有找到符合的bean,则回退为一个原始类型进行进行查找,如果找到就注入。
只是指定了@Resource注解的name,则按name后的名字去bean元素里查找有与之相等的name属性的bean。
只指定@Resource注解的type属性,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常。
@Autowired
@Autowired默认先按byType进行匹配,如果发现找到多个bean,则又按照byName方式进行匹配,如果还有多个,则报出异常。
@Resource注解的官方解释的更多相关文章
- @Autowired注解与@resource注解的区别(十分详细)
背景: 今天下班路上看到一个大货车,于是想到了装配,然后脑海里跳出了一个注解@Autowired(自动装配),于是又想到最近工作项目用的都是@Resource注解来进行装配.于是本着学什么东西都要一钻 ...
- 品Spring:对@Resource注解的处理方法
@Resource是Java的注解,表示一个资源,它具有双向的含义,一个是从外部获取一个资源,一个是向外部提供一个资源. 这其实就对应于Spring的注入和注册.当它用在字段和方法上时,表示前者.当它 ...
- Spring5:@Autowired注解、@Resource注解和@Service注解
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- Spring4.1.6 标签注解大全以及解释
常用的spring注解有如下几种: @Controller@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable ...
- 零宽度正预测先行断言是什么呢,看msdn上的官方解释定义
最近为了对html文件进行源码处理,需要进行正则查找并替换.于是借着这个机会把正则系统地学一下,虽然以前也用过正则,但每次都是临时学一下混过关的.在学习的过程中还是遇到不少问题的,特别是零宽断言(这里 ...
- 编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
首先分析. 1: 肯定要利用dom4j读取xml配置文件,将所有的bean的配置信息读取出来 2: 利用反射技术,实例化所有的bean 3: 写注解处理器, 利用注解和内省实现依赖对象的注入. 4: ...
- 转:Spring中@Autowired注解、@Resource注解的区别
Pay attention: When using these annotations, the object itself has to be created by Spring context. ...
- @Resource注解
原文地址:http://blog.sina.com.cn/s/blog_a795a96f01016if1.html @Resource 注解被用来激活一个命名资源(named resource)的 ...
随机推荐
- JXL读取Excel(只支持xls版本)——(二)
注意:jxl是不支持xlsx后缀的excel的.因此建议用POI读取excel. Jar包 同一一样 Java代码 package JXL; import java.io.File; import j ...
- Jetson tk1 hash sum mismatch
sudo apt-get update遭遇Hash Sum Mismatch 修改DNS服务器地址: sudo gedit /etc/resolv.conf 解决办法: 在装有goagent的情况下: ...
- ubuntu14.04 VIM for python 一键配置
# 超强vim配置文件 [](https://travis-ci. ...
- LibreOJ 题解汇总
目录 #1. A + B Problem #2. Hello, World! #3. Copycat #4. Quine #7. Input Test #100. 矩阵乘法 #101. 最大流 #10 ...
- Window和document的区别
1.window 窗口对象.就是可视化区域的大小,不包含滚动条内东东. 2.document 对象,包含滚动条以外的区域
- 三 、 Multivariance Linear Regssion练习(转载)
转载:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2962116.html 前言: 本文主要是来练习多变量线性回归问题(其实本文也就3个 ...
- The videobuf2 API【转】
转自:https://blog.csdn.net/paul_liao/article/details/8986999 The videobuf2 API Author:CJOK Contact:cjo ...
- linux 高级字符设备驱动 ioctl操作介绍 例程分析实现【转】
转自:http://my.oschina.net/u/274829/blog/285014 1,ioctl介绍 ioctl控制设备读写数据以及关闭等. 用户空间函数原型:int ioctl(int f ...
- cmake 使用
1.cmake 显示编译命令: 在顶层CMakeLists.txt里设置 set(CMAKE_VERBOSE_MAKEFILE ON) 或者 cmake . 再 m ...
- 【转】每天一个linux命令(1):ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写,缺省下ls用来打印出当前目录的清单.如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过ls命令不仅可以查看linux ...