溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么

發(fā)布時間:2023-03-06 15:13:49 來源:億速云 閱讀:106 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么文章都會有所收獲,下面我們一起來看看吧。

javax.annotation.Resource

jdk 內置的,JSR-250 中的注解。

依賴注入通過 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor 來處理。

org.springframework.beans.factory.annotation.Autowired
org.springframework.beans.factory.annotation.Value
javax.inject.Inject

JSR-330 中的注解,作用同 @Autowired

依賴注入通過 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 來處理。

org.springframework.beans.factory.annotation.Qualifier
javax.inject.Qualifier

依賴注入通過 org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver 來處理。

@Autowired

spring 自帶的注解。

注入順序

按照 type 在 上下文中查找匹配的 bean

如果有多個 bean,按照 name 進行匹配

  • 如果有 @Qualifier 注解,按照 @Qualifier 指定的 name 進行匹配

  • 如果沒有,按照變量名進行匹配

匹配不到,報錯。因為 required 默認為 true,不想注入設置此 bean @Autowired(required=false)。

@Inject

在 spring 中,@Inject 和 @Autowired 相同。

@Inject 和 @Autowired 區(qū)別

@Inject 是 javaee 6 及以上版本包里的。

@Autowired 可以設置 required=false 而 @Inject 沒有這個屬性。

@Resource

有兩個重要的屬性,name 和 type,spring 將 name 屬性解析為 bean 的名字,type 解析為 bean 的類型。如果未指定 name,取變量名給 name 賦值。

CommonAnnotationBeanPostProcessor 中Resource 賦值源碼

/**
     * Class representing injection information about an annotated field
     * or setter method, supporting the @Resource annotation.
     */
    private class ResourceElement extends LookupElement {
        private final boolean lazyLookup;
        public ResourceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) {
            super(member, pd);
            Resource resource = ae.getAnnotation(Resource.class);
            String resourceName = resource.name();
            Class<?> resourceType = resource.type();
            this.isDefaultName = !StringUtils.hasLength(resourceName);
            if (this.isDefaultName) {
                resourceName = this.member.getName();
                if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
                    resourceName = Introspector.decapitalize(resourceName.substring(3));
                }
            }
            else if (embeddedValueResolver != null) {
                resourceName = embeddedValueResolver.resolveStringValue(resourceName);
            }
            if (Object.class != resourceType) {
                checkResourceType(resourceType);
            }
            else {
                // No resource type specified... check field/method.
                resourceType = getResourceType();
            }
            this.name = (resourceName != null ? resourceName : "");
            this.lookupType = resourceType;
            String lookupValue = resource.lookup();
            this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());
            Lazy lazy = ae.getAnnotation(Lazy.class);
            this.lazyLookup = (lazy != null && lazy.value());
        }
        @Override
        protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) {
            return (this.lazyLookup ? buildLazyResourceProxy(this, requestingBeanName) :
                    getResource(this, requestingBeanName));
        }
    }

在變量名相同的情況下報錯

The bean could not be injected as a because it is a JDK dynamic proxy that implements:

指定了不同type無法解決問題,跟進源碼發(fā)現(xiàn)是 spring boot 把異常給處理了

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'productInit': 
Injection of resource dependencies failed; 
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'example2ProductMapper' is expected to be of type 'com.alibaba.cloud.youxia.manager.ProductManager' but was actually of type 'com.sun.proxy.$Proxy47'

想到在 DefaultListableBeanFactory 中 beanDefinitionMap 按照名稱和 BeanDefinition 鍵值對的問題,名稱和注入的對象一一對應,不然就會出現(xiàn)不對應的問題

注入規(guī)則

  • 如果未指定 name,取變量名從上下文中查找名稱匹配的 bean 進行注入,找不到或者注入的變量名與類型無法對應拋出異常。

  • 如果指定了 name,則從上下文中查找名稱匹配的 bean 進行注入,找不到拋出異常。

  • 如果指定了 type,有兩種情況

通過變量名從上下文中查找不到對應的 bean,則通過 type則從上下文中查找類型匹配的 bean 進行注入,找不到拋出異常。

通過變量名從上下文中找到對應的 bean但是注入的類型與無法與DefaultListableBeanFactory 中 beanDefinitionMap中通過變量名得到的 BeanDefinition 類型一致,拋出異常。

  • 既沒有指定 name,又沒有指定 type,默認按照變量名進行注入。

  • 如果同時指定了 name 和 type,從上下文中找到唯一匹配的 bean 進行注入,找不到拋出異常。

匹配順序為

變量名 &rarr; 指定的 name &rarr; 指定的 type

Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么

如果注入的 bean 變量名相同,但是類型不同,通過 name 指定是修改代碼量最小的辦法。

Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么

關于“Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Spring中@Autowired、@Resource和@Inject注解的區(qū)別是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI