zoukankan      html  css  js  c++  java
  • @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方式进行匹配,如果还有多个,则报出异常。

  • 相关阅读:
    web测试方法总结
    我认为测试应该掌握的SQL语句
    monkey(1)
    冒烟测试
    PC客户端测试总结
    常见测试点总结
    测试基本概念
    测试主要环节
    手机app常见bug积累
    MySQL面试题集锦
  • 原文地址:https://www.cnblogs.com/yuyu666/p/10060572.html
Copyright © 2011-2022 走看看