Spring中@Autowired @Resource @Inject三個(gè)注解有什么區(qū)別
javax.annotation.Resource
jdk 內(nèi)置的,JSR-250 中的注解。
依賴注入通過(guò) org.springframework.context.annotation.CommonAnnotationBeanPostProcessor 來(lái)處理。
org.springframework.beans.factory.annotation.Autowired org.springframework.beans.factory.annotation.Value
javax.inject.Inject
JSR-330 中的注解,作用同 @Autowired
依賴注入通過(guò) org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 來(lái)處理。
org.springframework.beans.factory.annotation.Qualifier javax.inject.Qualifier
依賴注入通過(guò) org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver 來(lái)處理。
@Autowired
spring 自帶的注解。
注入順序
按照 type 在 上下文中查找匹配的 bean
如果有多個(gè) bean,按照 name 進(jìn)行匹配
- 如果有 @Qualifier 注解,按照 @Qualifier 指定的 name 進(jìn)行匹配
- 如果沒(méi)有,按照變量名進(jìn)行匹配
匹配不到,報(bào)錯(cuò)。因?yàn)?required 默認(rèn)為 true,不想注入設(shè)置此 bean @Autowired(required=false)。
@Inject
在 spring 中,@Inject 和 @Autowired 相同。
@Inject 和 @Autowired 區(qū)別
@Inject 是 javaee 6 及以上版本包里的。
@Autowired 可以設(shè)置 required=false 而 @Inject 沒(méi)有這個(gè)屬性。
@Resource
有兩個(gè)重要的屬性,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));
}
}在變量名相同的情況下報(bào)錯(cuò)
The bean could not be injected as a because it is a JDK dynamic proxy that implements:
指定了不同type無(wú)法解決問(wèn)題,跟進(jìn)源碼發(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 鍵值對(duì)的問(wèn)題,名稱和注入的對(duì)象一一對(duì)應(yīng),不然就會(huì)出現(xiàn)不對(duì)應(yīng)的問(wèn)題
注入規(guī)則
- 如果未指定 name,取變量名從上下文中查找名稱匹配的 bean 進(jìn)行注入,找不到或者注入的變量名與類型無(wú)法對(duì)應(yīng)拋出異常。
- 如果指定了 name,則從上下文中查找名稱匹配的 bean 進(jìn)行注入,找不到拋出異常。
- 如果指定了 type,有兩種情況
通過(guò)變量名從上下文中查找不到對(duì)應(yīng)的 bean,則通過(guò) type則從上下文中查找類型匹配的 bean 進(jìn)行注入,找不到拋出異常。
通過(guò)變量名從上下文中找到對(duì)應(yīng)的 bean但是注入的類型與無(wú)法與DefaultListableBeanFactory 中 beanDefinitionMap中通過(guò)變量名得到的 BeanDefinition 類型一致,拋出異常。
- 既沒(méi)有指定 name,又沒(méi)有指定 type,默認(rèn)按照變量名進(jìn)行注入。
- 如果同時(shí)指定了 name 和 type,從上下文中找到唯一匹配的 bean 進(jìn)行注入,找不到拋出異常。
匹配順序?yàn)?/p>
變量名 → 指定的 name → 指定的 type

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

到此這篇關(guān)于Spring中@Autowired @Resource @Inject三個(gè)注解有什么區(qū)別的文章就介紹到這了,更多相關(guān)Spring @Autowired @Resource @Inject內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java調(diào)用第三方http接口的常用方式總結(jié)
這篇文章主要介紹了Java調(diào)用第三方http接口的常用方式總結(jié),具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot組件初始化后的4種啟動(dòng)方式及常用方法
在Spring Boot中,您可以通過(guò)幾種方式在組件初始化后執(zhí)行啟動(dòng)任務(wù),下面小編給大家分享springboot組件初始化后的4種啟動(dòng)方式及常用方法,感興趣的朋友一起看看吧2024-06-06
SpringBoot綠葉顯示yml和端口問(wèn)題及解決方法
今天是解決報(bào)錯(cuò)的一天,首先在操作Springboot中的時(shí)候,有些朋友的yml顯示的不是綠葉的圖標(biāo),或者是配置了之后不生效的問(wèn)題,今天就給大家分享SpringBoot綠葉顯示yml和端口問(wèn)題,感興趣的朋友一起看看吧2023-01-01
SpringData JPA中查詢接口Repository的使用
本文主要介紹了SpringData JPA中查詢接口Repository的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
MyBatis-Plus詳解(環(huán)境搭建、關(guān)聯(lián)操作)
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,今天通過(guò)本文給大家介紹MyBatis-Plus環(huán)境搭建及關(guān)聯(lián)操作,需要的朋友參考下吧2022-09-09
SpringCloud如何解決服務(wù)之間的通信問(wèn)題
本文主要介紹了SpringCloud如何解決服務(wù)之間的通信問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
java中面向?qū)ο蟮母拍罴爸R(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java中面向?qū)ο蟮母拍罴爸R(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考下。2021-01-01

