一、@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方式进行匹配,如果还有多个,则报出异常。

最新文章

  1. npm源切换
  2. HDU--航海舰队
  3. 读取手机上所有应用程序并显示(APP)
  4. rabbitmq消息队列——"工作队列"
  5. final、finally、finalize差异
  6. redis 编译安装(生产环境推荐)
  7. Java学习笔记——设计模式之四.代理模式
  8. How to call C/C++ sytle function from C# solution?
  9. 51nod 1035:最长的循环节
  10. SPP-Net理解
  11. installshield安装包制作
  12. Spark on Yarn with HA
  13. 在线版区间众数 hzw的代码。。
  14. PL/SQL学习笔记之包
  15. _Bool and bool
  16. Java第三方支付接入案例(支付宝)
  17. 3dmax2020下载安装3dmax2020破解中文版下载安装
  18. mysql 允许远程登录
  19. 哪个HTML5内建对象用于在画布上绘制?()
  20. OAuth2.0官方文档中文翻译

热门文章

  1. Informatic学习总结_day02_增量抽取
  2. Netty+SpringBoot写一个基于Http协议的文件服务器
  3. 【逆向知识】动态调试技巧-C++代码逆向
  4. BN讲解(转载)
  5. linux内核中链表代码分析---list.h头文件分析(一)【转】
  6. oracle instantclient_11_2 配置文件tnsnames.ora
  7. 路由器中继(repeater)模式 和 AP+WDS模式区别?
  8. 转载:Java的四种引用方式
  9. [学习笔记]Java的public,protected,private,缺省的作用域
  10. css之absolute