偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

終于有人把Autowired注解講清楚了,贊?。。?/h1>

開發(fā) 前端
Spring 容器會自動解析構造函數的參數類型,并為這些參數找到與其類型匹配的 Bean 實例,然后注入到構造函數中。

@Autowired是什么

@Autowired 注解由 Spring 的 org.springframework.beans.factory.annotation.Autowired 類定義, 直譯過來就是自動注入的意思。

@Autowired的定義如下:

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}

@Autowired 的使用場景

1.字段注入

將 @Autowired 直接應用于類的成員變量上。Spring 容器會自動為這些變量找到與其類型匹配的 Bean 實例,并進行注入。

public class MyClass {
    @Autowired
    private MyService myService;
}

2.構造器注入

將 @Autowired 應用于類的構造函數上。

Spring 容器會自動解析構造函數的參數類型,并為這些參數找到與其類型匹配的 Bean 實例,然后注入到構造函數中。

public class MyClass {
    private MyService myService;
    
    @Autowired
    public MyClass(MyService myService) {
        this.myService = myService;
    }
}

3.方法注入

將 @Autowired 應用于類的方法上。

當類實例化時,Spring 容器會自動解析這些方法的參數類型,并為這些參數找到與其類型匹配的 Bean 實例,然后調用這些方法并注入參數。

public class MyClass {
    private MyService myService;

    @Autowired
    public void setMyService(MyService myService) {
        this.myService = myService;
    }
}

需要注意的是,通過 @Autowired 注解實現依賴注入時,如果在 Spring 容器中找不到與某個依賴類型匹配的 Bean 實例(或者找到多個,但沒有明確的優(yōu)先級),那么 Spring 將拋出異常。

除非將該注解的 required 屬性設置為 false,這樣在找不到匹配的 Bean 時,框架將不會拋出異常。

public class MyClass {
    @Autowired(required = false)
    private MyService myService;
}

@Autowired是如何工作的

在 Spring 中,AutowiredAnnotationBeanPostProcessor (AABP) 負責處理帶有 @Autowired 注解的成員變量、Setter 方法。

以下是 AABP 解析 @Autowired 的完整代碼調用流程:

當 Spring 容器實例化一個 Bean 時,會創(chuàng)建相應的 BeanDefinition 對象。BeanDefinition 包含了關于 Bean 的所有元數據信息。

在容器實例化、配置和初始化 Bean 的過程中,它會調用 AABP 的 postProcessMergedBeanDefinition 方法,以收集與依賴注入相關的元數據。

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 方法會查找 Bean 的所有@Autowired 注解相關的元數據,并獲取 InjectionMetadata 對象, 如果該對象尚不存在,會創(chuàng)建一個新的對象。

protected InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
    // ... (省略無關代碼)

    List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
    Class<?> targetClass = clazz;

    // 遍歷 Bean 的類結構,從子類向基類查找有@Autowired 注解的字段、方法和構造器
    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)字段不能自動注入
                    // ... (省略錯誤處理和日志)
                }
                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)方法不能自動注入
                    // ... (省略錯誤處理和日志)
                }
                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);

    // 構建并返回 InjectionMetadata 對象
    return new InjectionMetadata(clazz, elements);
}

上面的代碼中,我在關鍵位置添加了注釋,老鐵們可以仔細看一下,上述代碼的主要作用就是找到一個類中:

  • 添加了@Autowired的屬性信息,用 AutowiredFieldElement進行表示。
  • 添加了 @Autowired 的方法信息,用AutowiredMethodElement進行表示。

當依賴注入需要發(fā)生時,容器會調用 AABP 的 postProcessProperties 方法。

該方法中會調用 InjectionMetadata 的 inject 方法來實際注入 @Autowired 注解的成員變量、成員方法:

metadata.inject(bean, beanName, pvs);

最后,通過執(zhí)行 AutowiredFieldElement 和 AutowiredMethodElement 的 inject 方法來實際注入屬性值和方法參數。

AutowiredFieldElement 的 inject 方法實現如下:

@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 方法的實現如下:

@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();
    }
   }
  }

通過以上流程,AutowiredAnnotationBeanPostProcessor 將解析并注入帶有 @Autowired 注解的成員變量、方法。

責任編輯:武曉燕 來源: 小李哥編程
相關推薦

2020-07-29 09:21:34

Docker集群部署隔離環(huán)境

2021-07-05 22:22:24

協(xié)議MQTT

2019-07-07 08:18:10

MySQL索引數據庫

2021-04-10 10:37:04

OSITCP互聯(lián)網

2020-12-24 15:18:27

大數據數據分析

2023-08-14 11:35:16

流程式轉化率數據指標

2019-05-22 08:43:45

指令集RISC-V開源

2024-02-23 08:08:21

2022-01-05 09:27:24

讀擴散寫擴散feed

2020-10-16 17:20:21

索引MySQL數據庫

2021-01-29 10:50:04

數據中臺數據數據管理

2024-02-27 14:27:16

2019-07-04 09:13:04

中臺百度團隊

2021-02-25 08:21:38

高可用風險故障

2020-04-23 10:21:57

Linux 網絡編程 數據

2020-03-02 15:17:37

云原生CNCF容器

2024-07-01 13:45:18

2019-06-19 14:58:38

服務器負載均衡客戶端

2020-10-29 10:35:53

Nginx架構服務器

2022-07-04 11:27:02

標簽數據指標標簽體系
點贊
收藏

51CTO技術棧公眾號