來(lái)源:blog.csdn.net/topdeveloperr/article/details/87971446
使用spring開(kāi)發(fā)時(shí),進(jìn)行配置主要有兩種方式,一是xml的方式,二是java config的方式。
spring技術(shù)自身也在不斷的發(fā)展和改變,從當(dāng)前springboot的火熱程度來(lái)看,java config的應(yīng)用是越來(lái)越廣泛了,在使用java config的過(guò)程當(dāng)中,我們不可避免的會(huì)有各種各樣的注解打交道,其中,我們使用最多的注解應(yīng)該就是@Autowired注解了。這個(gè)注解的功能就是為我們注入一個(gè)定義好的bean。
那么,這個(gè)注解除了我們常用的屬性注入方式之外還有哪些使用方式呢?它在代碼層面又是怎么實(shí)現(xiàn)的呢?這是本篇文章著重想討論的問(wèn)題。
在分析這個(gè)注解的實(shí)現(xiàn)原理之前,我們不妨先來(lái)回顧一下@Autowired注解的用法。
將@Autowired注解應(yīng)用于構(gòu)造函數(shù),如以下示例所示
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
將@Autowired注釋?xiě)?yīng)用于setter方法
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
將@Autowired注釋?xiě)?yīng)用于具有任意名稱和多個(gè)參數(shù)的方法
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
您也可以將@Autowired應(yīng)用于字段,或者將其與構(gòu)造函數(shù)混合,如以下示例所示
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
直接應(yīng)用于字段是我們使用的最多的一種方式,但是使用構(gòu)造方法注入從代碼層面卻是更加好的。除此之外,還有以下不太常見(jiàn)的幾種方式
將@Autowired注釋添加到需要該類型數(shù)組的字段或方法,則spring會(huì)從ApplicationContext中搜尋符合指定類型的所有bean,如以下示例所示:
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
數(shù)組可以,我們可以馬上舉一反三,那容器也可以嗎,答案是肯定的,下面是set以及map的例子:
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
public class MovieRecommender {
private Map<String, MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
以上就是@Autowired注解的主要使用方式,經(jīng)常使用spring的話應(yīng)該對(duì)其中常用的幾種不會(huì)感到陌生。
@Autowired這個(gè)注解我們經(jīng)常在使用,現(xiàn)在,我想問(wèn)的是,它的作用到底是什么呢?
首先,我們從所屬范圍來(lái)看,事實(shí)上這個(gè)注解是屬于spring的容器配置的一個(gè)注解,與它同屬容器配置的注解還有:@Required,@Primary, @Qualifier等等。因此@Autowired注解是一個(gè)用于容器(container)配置的注解。
其次,我們可以直接從字面意思來(lái)看,@autowired注解來(lái)源于英文單詞autowire,這個(gè)單詞的意思是自動(dòng)裝配的意思。自動(dòng)裝配又是什么意思?這個(gè)詞語(yǔ)本來(lái)的意思是指的一些工業(yè)上的用機(jī)器代替人口,自動(dòng)將一些需要完成的組裝任務(wù),或者別的一些任務(wù)完成。而在spring的世界當(dāng)中,自動(dòng)裝配指的就是使用將Spring容器中的bean自動(dòng)的和我們需要這個(gè)bean的類組裝在一起。
因此,筆者個(gè)人對(duì)這個(gè)注解的作用下的定義就是:將Spring容器中的bean自動(dòng)的和我們需要這個(gè)bean的類組裝在一起協(xié)同使用。
接下來(lái),我們就來(lái)看一下這個(gè)注解背后到底做了些什么工作。
事實(shí)上,要回答這個(gè)問(wèn)題必須先弄明白的是java是如何支持注解這樣一個(gè)功能的。
java的注解實(shí)現(xiàn)的核心技術(shù)是反射,讓我們通過(guò)一些例子以及自己實(shí)現(xiàn)一個(gè)注解來(lái)理解它工作的原理。
@Override注解的定義如下:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@Override注解使用java官方提供的注解,它的定義里面并沒(méi)有任何的實(shí)現(xiàn)邏輯。注意,所有的注解幾乎都是這樣的,注解只能是被看作元數(shù)據(jù),它不包含任何業(yè)務(wù)邏輯。 注解更像是一個(gè)標(biāo)簽,一個(gè)聲明,表面被注釋的這個(gè)地方,將具有某種特定的邏輯。
那么,問(wèn)題接踵而至,注解本身不包含任何邏輯,那么注解的功能是如何實(shí)現(xiàn)的呢?答案必然是別的某個(gè)地方對(duì)這個(gè)注解做了實(shí)現(xiàn)。以@Override注解為例,他的功能是重寫(xiě)一個(gè)方法,而他的實(shí)現(xiàn)者就是JVM,java虛擬機(jī),java虛擬機(jī)在字節(jié)碼層面實(shí)現(xiàn)了這個(gè)功能。
但是對(duì)于開(kāi)發(fā)人員,虛擬機(jī)的實(shí)現(xiàn)是無(wú)法控制的東西,也不能用于自定義注解。所以,如果是我們自己想定義一個(gè)獨(dú)一無(wú)二的注解的話,則我們需要自己為注解寫(xiě)一個(gè)實(shí)現(xiàn)邏輯,換言之,我們需要實(shí)現(xiàn)自己注解特定邏輯的功能。
在自己寫(xiě)注解之前我們有一些基礎(chǔ)知識(shí)需要掌握,那就是我們寫(xiě)注解這個(gè)功能首先是需要java支持的,java在jdk5當(dāng)中支持了這一功能,并且在java.lang.annotation
包中提供了四個(gè)注解,僅用于編寫(xiě)注解時(shí)使用,他們是:
下面我們開(kāi)始自己實(shí)現(xiàn)一個(gè)注解,注解僅支持 primitives
, string
和 enumerations
這三種類型。注解的所有屬性都定義為方法,也可以提供默認(rèn)值。我們先實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的注解。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SimpleAnnotation {
String value();
}
上面這個(gè)注釋里面只定義了一個(gè)字符傳,它的目標(biāo)注釋對(duì)象是方法,保留策略是在運(yùn)行期間。下面我們定義一個(gè)方法來(lái)使用這個(gè)注解:
public class UseAnnotation {
@SimpleAnnotation("testStringValue")
public void testMethod(){
//do something here
}
}
我們?cè)谶@里使用了這個(gè)注解,并把字符串賦值為:testStringValue,到這里,定義一個(gè)注解并使用它,我們就已經(jīng)全部完成。
簡(jiǎn)單的不敢相信。但是,細(xì)心一想的話,我們雖然寫(xiě)了一個(gè)注解也用了它,可是它并沒(méi)有產(chǎn)生任何作用啊。也沒(méi)有對(duì)我們這里方法產(chǎn)生任何效果啊。是的現(xiàn)在確實(shí)是這樣的,原因在于我們前面提到的一點(diǎn),我們還沒(méi)有為這個(gè)注解實(shí)現(xiàn)它的邏輯,現(xiàn)在我們就來(lái)為這個(gè)注解實(shí)現(xiàn)邏輯。
應(yīng)該怎么做呢?我們不妨自己來(lái)想一想。首先,我想給標(biāo)注了這個(gè)注解的方法或字段實(shí)現(xiàn)功能,我們必須得知道,到底有哪些方法,哪些字段使用了這個(gè)注解吧,因此,這里我們很容易想到,這里應(yīng)該會(huì)用到反射。
其次,利用反射,我們利用反射拿到這樣目標(biāo)之后,得為他實(shí)現(xiàn)一個(gè)邏輯,這個(gè)邏輯是這些方法本身邏輯之外的邏輯,這又讓我們想起了代理,aop等知識(shí),我們相當(dāng)于就是在為這些方法做一個(gè)增強(qiáng)。事實(shí)上的實(shí)現(xiàn)主借的邏輯也大概就是這個(gè)思路。梳理一下大致步驟如下:
現(xiàn)在我們來(lái)實(shí)現(xiàn)一下這個(gè)邏輯,代碼如下:
private static void annotationLogic() {
Class useAnnotationClass = UseAnnotation.class;
for(Method method : useAnnotationClass.getMethods()) {
SimpleAnnotation simpleAnnotation = (SimpleAnnotation)method.getAnnotation(SimpleAnnotation.class);
if(simpleAnnotation != null) {
System.out.println(" Method Name : " + method.getName());
System.out.println(" value : " + simpleAnnotation.value());
System.out.println(" --------------------------- ");
}
}
}
在這里我們實(shí)現(xiàn)的邏輯就是打印幾句話。從上面的實(shí)現(xiàn)邏輯我們不能發(fā)現(xiàn),借助于java的反射我們可以直接拿到一個(gè)類里所有的方法,然后再拿到方法上的注解,當(dāng)然,我們也可以拿到字段上的注解。借助于反射我們可以拿到幾乎任何屬于一個(gè)類的東西。
一個(gè)簡(jiǎn)單的注解我們就實(shí)現(xiàn)完了。現(xiàn)在我們?cè)倩剡^(guò)頭來(lái),看一下@Autowired注解是如何實(shí)現(xiàn)的。
知道了上面的知識(shí),我們不難想到,上面的注解雖然簡(jiǎn)單,但是@Autowired和他最大的區(qū)別應(yīng)該僅僅在于注解的實(shí)現(xiàn)邏輯,其他利用反射獲取注解等等步驟應(yīng)該都是一致的。先來(lái)看一下@Autowired這個(gè)注解在spring的源代碼里的定義是怎樣的,如下所示:
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
閱讀代碼我們可以看到,Autowired注解可以應(yīng)用在構(gòu)造方法,普通方法,參數(shù),字段,以及注解這五種類型的地方,它的保留策略是在運(yùn)行時(shí)。下面,我們不多說(shuō)直接來(lái)看spring對(duì)這個(gè)注解進(jìn)行的邏輯實(shí)現(xiàn).
在Spring源代碼當(dāng)中,Autowired注解位于包org.springframework.beans.factory.annotation
之中,該包的內(nèi)容如下:
經(jīng)過(guò)分析,不難發(fā)現(xiàn)Spring對(duì)autowire注解的實(shí)現(xiàn)邏輯位于類:AutowiredAnnotationBeanPostProcessor
之中,已在上圖標(biāo)紅。其中的核心處理代碼如下:
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
Class<?> targetClass = clazz;//需要處理的目標(biāo)類
do {
final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
/*通過(guò)反射獲取該類所有的字段,并遍歷每一個(gè)字段,并通過(guò)方法findAutowiredAnnotation遍歷每一個(gè)字段的所用注解,并如果用autowired修飾了,則返回auotowired相關(guān)屬性*/
ReflectionUtils.doWithLocalFields(targetClass, field -> {
AnnotationAttributes ann = findAutowiredAnnotation(field);
if (ann != null) {//校驗(yàn)autowired注解是否用在了static方法上
if (Modifier.isStatic(field.getModifiers())) {
if (logger.isWarnEnabled()) {
logger.warn("Autowired annotation is not supported on static fields: " + field);
}
return;
}//判斷是否指定了required
boolean required = determineRequiredStatus(ann);
currElements.add(new AutowiredFieldElement(field, required));
}
});
//和上面一樣的邏輯,但是是通過(guò)反射處理類的method
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
return;
}
AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) {
if (logger.isWarnEnabled()) {
logger.warn("Autowired annotation is not supported on static methods: " + method);
}
return;
}
if (method.getParameterCount() == 0) {
if (logger.isWarnEnabled()) {
logger.warn("Autowired annotation should only be used on methods with parameters: " +
method);
}
}
boolean required = determineRequiredStatus(ann);
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
currElements.add(new AutowiredMethodElement(method, required, pd));
}
});
//用@Autowired修飾的注解可能不止一個(gè),因此都加在currElements這個(gè)容器里面,一起處理
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
return new InjectionMetadata(clazz, elements);
}
博主在源代碼里加了注釋,結(jié)合注釋就能看懂它做的事情了,最后這個(gè)方法返回的就是包含所有帶有autowire注解修飾的一個(gè)InjectionMetadata集合。這個(gè)類由兩部分組成:
public InjectionMetadata(Class<?> targetClass, Collection<InjectedElement> elements) {
this.targetClass = targetClass;
this.injectedElements = elements;
}
一是我們處理的目標(biāo)類,二就是上述方法獲取到的所以elements集合。
有了目標(biāo)類,與所有需要注入的元素集合之后,我們就可以實(shí)現(xiàn)autowired的依賴注入邏輯了,實(shí)現(xiàn)的方法如下:
@Override
public PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
try {
metadata.inject(bean, beanName, pvs);
}
catch (BeanCreationException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
}
return pvs;
}
它調(diào)用的方法是InjectionMetadata中定義的inject方法,如下
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Collection<InjectedElement> checkedElements = this.checkedElements;
Collection<InjectedElement> elementsToIterate =
(checkedElements != null ? checkedElements : this.injectedElements);
if (!elementsToIterate.isEmpty()) {
for (InjectedElement element : elementsToIterate) {
if (logger.isTraceEnabled()) {
logger.trace("Processing injected element of bean '" + beanName + "': " + element);
}
element.inject(target, beanName, pvs);
}
}
}
其邏輯就是遍歷,然后調(diào)用inject方法,inject方法其實(shí)現(xiàn)邏輯如下:
/**
* Either this or {@link #getResourceToInject} needs to be overridden.
*/
protected void inject(Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs)
throws Throwable {
if (this.isField) {
Field field = (Field) this.member;
ReflectionUtils.makeAccessible(field);
field.set(target, getResourceToInject(target, requestingBeanName));
}
else {
if (checkPropertySkipping(pvs)) {
return;
}
try {
Method method = (Method) this.member;
ReflectionUtils.makeAccessible(method);
method.invoke(target, getResourceToInject(target, requestingBeanName));
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
在這里的代碼當(dāng)中我們也可以看到,是inject也使用了反射技術(shù)并且依然是分成字段和方法去處理的。在代碼里面也調(diào)用了makeAccessible這樣的可以稱之為暴力破解的方法,但是反射技術(shù)本就是為框架等用途設(shè)計(jì)的,這也無(wú)可厚非。
對(duì)于字段的話,本質(zhì)上就是去set這個(gè)字段的值,即對(duì)對(duì)象進(jìn)行實(shí)例化和賦值,例如下面代碼:
@Autowired
ObjectTest objectTest;
那么在這里實(shí)現(xiàn)的就相當(dāng)于給這個(gè)objecTest引用賦值了。
對(duì)于方法的話,本質(zhì)就是去調(diào)用這個(gè)方法,因此這里調(diào)用的是method.invoke.
getResourceToInject方法的參數(shù)就是要注入的bean的名字,這個(gè)方法的功能就是根據(jù)這個(gè)bean的名字去拿到它。
以上,就是@Autowire注解實(shí)現(xiàn)邏輯的全部分析。結(jié)合源代碼再看一遍的話,會(huì)更加清楚一點(diǎn)。下面是spring容器如何實(shí)現(xiàn)@AutoWired自動(dòng)注入的過(guò)程的圖:
總結(jié)起來(lái)一句話:使用@Autowired注入的bean對(duì)于目標(biāo)類來(lái)說(shuō),從代碼結(jié)構(gòu)上來(lái)講也就是一個(gè)普通的成員變量,@Autowired和spring一起工作,通過(guò)反射為這個(gè)成員變量賦值,也就是將其賦為期望的類實(shí)例。另外,關(guān)注Java知音公眾號(hào),回復(fù)“后端面試”,送你一份面試題寶典!
各種注釋之間的第一個(gè)主要區(qū)別是,它們是在編譯時(shí)使用,然后被丟棄(如@Override),還是被放在編譯的類文件中,并在運(yùn)行時(shí)可用(如Spring的@Component)。這是由注釋的“@Retention”策略決定的。如果您正在編寫(xiě)自己的注釋,則需要決定該注釋在運(yùn)行時(shí)(可能用于自動(dòng)配置)還是僅在編譯時(shí)(用于檢查或代碼生成)有用。
當(dāng)用注釋編譯代碼時(shí),編譯器看到注釋就像看到源元素上的其他修飾符一樣,比如訪問(wèn)修飾符(public/private)或.。當(dāng)遇到注釋時(shí),它運(yùn)行一個(gè)注釋處理器,就像一個(gè)插件類,表示對(duì)特定的注釋感興趣。注釋處理器通常使用反射API來(lái)檢查正在編譯的元素,并且可以簡(jiǎn)單地對(duì)它們執(zhí)行檢查、修改它們或生成要編譯的新代碼。
@Override是一個(gè)示例;它使用反射API來(lái)確保能夠在其中一個(gè)超類中找到方法簽名的匹配,如果不能,則使用@Override會(huì)導(dǎo)致編譯錯(cuò)誤。
無(wú)論以何種方式注入,注入的bean就相當(dāng)于類中的一個(gè)普通對(duì)象應(yīng)用,這是它的實(shí)例化是spring去容器中找符合的bean進(jìn)行實(shí)例化,并注入到類當(dāng)中的。他們之間的關(guān)系就是普通的一個(gè)對(duì)象持有另一個(gè)對(duì)象引用的關(guān)系。只是這些對(duì)象都是spring當(dāng)中的bean而已。
從設(shè)計(jì)的角度來(lái)說(shuō) ,使用靜態(tài)字段會(huì)鼓勵(lì)使用靜態(tài)方法。靜態(tài)方法是evil的。依賴注入的主要目的是讓容器為您創(chuàng)建對(duì)象并進(jìn)行連接。而且,它使測(cè)試更加容易。
一旦開(kāi)始使用靜態(tài)方法,您就不再需要?jiǎng)?chuàng)建對(duì)象的實(shí)例,并且測(cè)試變得更加困難。同樣,您不能創(chuàng)建給定類的多個(gè)實(shí)例,每個(gè)實(shí)例都注入不同的依賴項(xiàng)(因?yàn)樵撟侄问请[式共享的,并且會(huì)創(chuàng)建全局狀態(tài))。
靜態(tài)變量不是Object的屬性,而是Class的屬性。spring的autowire是在對(duì)象上完成的,這樣使得設(shè)計(jì)很干凈。 在spring當(dāng)中我們也可以將bean對(duì)象定義為單例,這樣就能從功能上實(shí)現(xiàn)與靜態(tài)定義相同的目的。
但是從純粹技術(shù)的層面,我們可以這樣做:
將@Autowired可以與setter方法一起使用,然后可以讓setter修改靜態(tài)字段的值。但是這種做法非常不推薦。
推薦3個(gè)原創(chuàng)springboot+Vue項(xiàng)目,有完整視頻講解與文檔和源碼:
視頻講解:https://www.bilibili.com/video/BV1Jq4y1w7Bc/
完整開(kāi)發(fā)文檔:https://www.zhuawaba.com/post/124
線上演示:https://www.zhuawaba.com/dailyhub
視頻講解:https://www.bilibili.com/video/BV1af4y1s7Wh/
完整開(kāi)發(fā)文檔前端:https://www.zhuawaba.com/post/18
完整開(kāi)發(fā)文檔后端:https://www.zhuawaba.com/post/19
線上演示:https://www.markerhub.com/vueadmin/
視頻講解:https://www.bilibili.com/video/BV1PQ4y1P7hZ
完整開(kāi)發(fā)文檔:https://www.zhuawaba.com/post/17
---
聯(lián)系客服