終于有人把Autowired注解講清楚了,贊?。?!
@Autowired是什么
@Autowired 注解由 Spring 的 org.springframework.beans.factory.annotation.Autowired 類定義, 直譯過(guò)來(lái)就是自動(dòng)注入的意思。
@Autowired的定義如下:
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
@Autowired 的使用場(chǎng)景
1.字段注入
將 @Autowired 直接應(yīng)用于類的成員變量上。Spring 容器會(huì)自動(dòng)為這些變量找到與其類型匹配的 Bean 實(shí)例,并進(jìn)行注入。
public class MyClass {
@Autowired
private MyService myService;
}
2.構(gòu)造器注入
將 @Autowired 應(yīng)用于類的構(gòu)造函數(shù)上。
Spring 容器會(huì)自動(dòng)解析構(gòu)造函數(shù)的參數(shù)類型,并為這些參數(shù)找到與其類型匹配的 Bean 實(shí)例,然后注入到構(gòu)造函數(shù)中。
public class MyClass {
private MyService myService;
@Autowired
public MyClass(MyService myService) {
this.myService = myService;
}
}
3.方法注入
將 @Autowired 應(yīng)用于類的方法上。
當(dāng)類實(shí)例化時(shí),Spring 容器會(huì)自動(dòng)解析這些方法的參數(shù)類型,并為這些參數(shù)找到與其類型匹配的 Bean 實(shí)例,然后調(diào)用這些方法并注入?yún)?shù)。
public class MyClass {
private MyService myService;
@Autowired
public void setMyService(MyService myService) {
this.myService = myService;
}
}
需要注意的是,通過(guò) @Autowired 注解實(shí)現(xiàn)依賴注入時(shí),如果在 Spring 容器中找不到與某個(gè)依賴類型匹配的 Bean 實(shí)例(或者找到多個(gè),但沒(méi)有明確的優(yōu)先級(jí)),那么 Spring 將拋出異常。
除非將該注解的 required 屬性設(shè)置為 false,這樣在找不到匹配的 Bean 時(shí),框架將不會(huì)拋出異常。
public class MyClass {
@Autowired(required = false)
private MyService myService;
}
@Autowired是如何工作的
在 Spring 中,AutowiredAnnotationBeanPostProcessor (AABP) 負(fù)責(zé)處理帶有 @Autowired 注解的成員變量、Setter 方法。
以下是 AABP 解析 @Autowired 的完整代碼調(diào)用流程:
當(dāng) Spring 容器實(shí)例化一個(gè) Bean 時(shí),會(huì)創(chuàng)建相應(yīng)的 BeanDefinition 對(duì)象。BeanDefinition 包含了關(guān)于 Bean 的所有元數(shù)據(jù)信息。
在容器實(shí)例化、配置和初始化 Bean 的過(guò)程中,它會(huì)調(diào)用 AABP 的 postProcessMergedBeanDefinition 方法,以收集與依賴注入相關(guān)的元數(shù)據(jù)。
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
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;
}
findAutowiringMetadata 方法會(huì)查找 Bean 的所有@Autowired 注解相關(guān)的元數(shù)據(jù),并獲取 InjectionMetadata 對(duì)象, 如果該對(duì)象尚不存在,會(huì)創(chuàng)建一個(gè)新的對(duì)象。
protected InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
// ... (省略無(wú)關(guān)代碼)
List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Class<?> targetClass = clazz;
// 遍歷 Bean 的類結(jié)構(gòu),從子類向基類查找有@Autowired 注解的字段、方法和構(gòu)造器
do {
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
ReflectionUtils.doWithLocalFields(targetClass, field -> {
// 尋找?guī)в蠤Autowired 注解的字段
MergedAnnotation<?> ann = findAutowiredAnnotation(field);
if (ann != null) {
if (Modifier.isStatic(field.getModifiers())) {
// 靜態(tài)字段不能自動(dòng)注入
// ... (省略錯(cuò)誤處理和日志)
}
boolean required = determineRequiredStatus(ann);
// AutowiredFieldElement 屬性Autowired元素
currElements.add(new AutowiredFieldElement(field, required));
}
});
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
// 尋找?guī)в蠤Autowired 注解的Setter方法或普通方法
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
return;
}
MergedAnnotation<?> ann = findAutowiredAnnotation(bridgedMethod);
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) {
// 靜態(tài)方法不能自動(dòng)注入
// ... (省略錯(cuò)誤處理和日志)
}
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
boolean required = determineRequiredStatus(ann);
// AutowiredMethodElement 方法 Autowired 元素
currElements.add(new AutowiredMethodElement(method, required, pd));
}
});
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
// 構(gòu)建并返回 InjectionMetadata 對(duì)象
return new InjectionMetadata(clazz, elements);
}
上面的代碼中,我在關(guān)鍵位置添加了注釋,老鐵們可以仔細(xì)看一下,上述代碼的主要作用就是找到一個(gè)類中:
- 添加了@Autowired的屬性信息,用 AutowiredFieldElement進(jìn)行表示。
- 添加了 @Autowired 的方法信息,用AutowiredMethodElement進(jìn)行表示。
當(dāng)依賴注入需要發(fā)生時(shí),容器會(huì)調(diào)用 AABP 的 postProcessProperties 方法。
該方法中會(huì)調(diào)用 InjectionMetadata 的 inject 方法來(lái)實(shí)際注入 @Autowired 注解的成員變量、成員方法:
metadata.inject(bean, beanName, pvs);
最后,通過(guò)執(zhí)行 AutowiredFieldElement 和 AutowiredMethodElement 的 inject 方法來(lái)實(shí)際注入屬性值和方法參數(shù)。
AutowiredFieldElement 的 inject 方法實(shí)現(xiàn)如下:
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Field field = (Field) this.member;
Object value;
if (this.cached) {
try {
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
}
catch (NoSuchBeanDefinitionException ex) {
// Unexpected removal of target bean for cached argument -> re-resolve
value = resolveFieldValue(field, bean, beanName);
}
}
else {
value = resolveFieldValue(field, bean, beanName);
}
if (value != null) {
ReflectionUtils.makeAccessible(field);
field.set(bean, value);
}
}
AutowiredMethodElement 的 inject 方法的實(shí)現(xiàn)如下:
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
if (checkPropertySkipping(pvs)) {
return;
}
Method method = (Method) this.member;
Object[] arguments;
if (this.cached) {
try {
arguments = resolveCachedArguments(beanName);
}
catch (NoSuchBeanDefinitionException ex) {
// Unexpected removal of target bean for cached argument -> re-resolve
arguments = resolveMethodArguments(method, bean, beanName);
}
}
else {
arguments = resolveMethodArguments(method, bean, beanName);
}
if (arguments != null) {
try {
ReflectionUtils.makeAccessible(method);
method.invoke(bean, arguments);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
通過(guò)以上流程,AutowiredAnnotationBeanPostProcessor 將解析并注入帶有 @Autowired 注解的成員變量、方法。