亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot中@Autowired生效方式詳解

 更新時(shí)間:2022年06月17日 10:19:38   作者:alonwang  
@Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準(zhǔn)確地控制bean在何處如何自動(dòng)裝配的過程。在默認(rèn)情況下,該注解是類型驅(qū)動(dòng)的注入

前言

@Component
public class SimpleBean3 {
    @Autowired
    private SimpleBean simpleBean;
}

@Autowired修飾的字段會(huì)被容器自動(dòng)注入.那么Spring Boot中使如何實(shí)現(xiàn)這一功能呢? AutowiredAnnotationBeanPostProcessor!

BeanPostProcessor implementation that autowires annotated fields, setter methods, and arbitrary config methods. Such members to be injected are detected through annotations: by default, Spring's @Autowired and @Value annotations.
Also supports JSR-330's @Inject annotation, if available, as a direct alternative to Spring's own @Autowired.

AutowiredAnnotationBeanPostProcessor(以下簡稱AutowiredProcessor)間接實(shí)現(xiàn)了InstantiationAwareBeanPostProcessor接口.通過postProcessProperties(...)完成@Autowired的注入,本文將按照下圖流程梳理AutowiredProcessor的生效邏輯.

SpringBoot-autowired.png

正文

注冊AutowiredProcessor的BeanDefinition

SpringApplication#createApplicationContext默認(rèn)會(huì)創(chuàng)建 AnnotationConfigApplicationContext,而AnnotationConfigApplicationContext又會(huì)創(chuàng)建AnnotatedBeanDefinitionReader

    public AnnotationConfigApplicationContext() {
        this.reader = new AnnotatedBeanDefinitionReader(this);
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

AnnotatedBeanDefinitionReader構(gòu)造時(shí)會(huì)調(diào)用AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry),將AutowiredProcessor的BeanDefinition注冊到容器

    public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
            BeanDefinitionRegistry registry, @Nullable Object source) {
        //忽略部分代碼...
        if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
            RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
            def.setSource(source);
            beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
        }
        //忽略部分代碼...
        return beanDefs;
    }

實(shí)例化AutowiredProcessor

在AbstractApplicationContext的refresh階段,會(huì)注冊并實(shí)例化所有的BeanPostProcessor

public void refresh() throws BeansException, IllegalStateException {
            //...忽略部分代碼
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);
                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);
                // ########### 這里注冊所有的BeanPostProcessor ##########  
                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);
                // Initialize message source for this context.
                initMessageSource();
                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();
                // Initialize other special beans in specific context subclasses.
                onRefresh();
                // Check for listener beans and register them.
                registerListeners();
                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);
                // Last step: publish corresponding event.
                finishRefresh()
            //...忽略部分代碼
    }

實(shí)際的注冊邏輯交給了PostProcessorRegistrationDelegate

    protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
    }

在PostProcessorRegistrationDelegate中,獲取到所有的BeanPostProcessor(基于BeanDefinition),并將其分為幾種類型,并按照不同的優(yōu)先級進(jìn)行處理化,這塊不是這篇文章的重點(diǎn),我們只需要知道在這里AutowiredProcessor被注冊就可以了.

創(chuàng)建bean時(shí)進(jìn)行注入

以SimpleBean3的注入為例, 它是單例的,在AbstractApplicationContext.refresh()的finishBeanFactoryInitialization(beanFactory)時(shí)創(chuàng)建.

    protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
        //...忽略部分代碼
        // Instantiate all remaining (non-lazy-init) singletons.
        beanFactory.preInstantiateSingletons();
    }

調(diào)用到了BeanFactory.preInstantiateSingletons(),走到getBean()邏輯

public void preInstantiateSingletons() throws BeansException {
        //...忽略部分代碼
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
        // Trigger initialization of all non-lazy singleton beans...
        for (String beanName : beanNames) {
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                if (isFactoryBean(beanName)) {
                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                    if (bean instanceof FactoryBean) {
                        final FactoryBean<?> factory = (FactoryBean<?>) bean;
                        boolean isEagerInit;
                        if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                            isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
                                            ((SmartFactoryBean<?>) factory)::isEagerInit,
                                    getAccessControlContext());
                        }
                        else {
                            isEagerInit = (factory instanceof SmartFactoryBean &&
                                    ((SmartFactoryBean<?>) factory).isEagerInit());
                        }
                        if (isEagerInit) {
                            getBean(beanName);
                        }
                    }
                }
                else {
                    getBean(beanName);
                }
            }
        }
        //...忽略部分代碼
    }

經(jīng)過一連串的輾轉(zhuǎn),最終調(diào)用到AbstractAutowireCapableBeanFactory#populateBean

附上調(diào)用鏈路

image.png

在populateBean中,會(huì)將所有的BeanPostProcessor應(yīng)用在這個(gè)bean上,包括AutowiredProcessor

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
        //...忽略部分代碼
        PropertyDescriptor[] filteredPds = null;
        if (hasInstAwareBpps) {
            if (pvs == null) {
                pvs = mbd.getPropertyValues();
            }
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
          //###### 調(diào)用到postProcessProperties #####
                    PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
                    if (pvsToUse == null) {
                        if (filteredPds == null) {
                            filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
                        }
                        pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                        if (pvsToUse == null) {
                            return;
                        }
                    }
                    pvs = pvsToUse;
                }
            }
        }
    //...忽略部分代碼
    }

AutowiredProcessor的postProcessProperties()會(huì)進(jìn)行注入操作,這需要找到注入的元數(shù)據(jù)(InjectionMetadata)

public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {     
        //### 找到AutowringMetadata #####
        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()又調(diào)用到buildAutowiringMetadata(),生成代表可注入元素的InjectMetadata

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
        List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
        Class<?> targetClass = clazz;
        do {
            final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
            ReflectionUtils.doWithLocalFields(targetClass, field -> {
        //###### 找到帶有可注入注解的字段
                AnnotationAttributes ann = findAutowiredAnnotation(field);
                if (ann != null) {
                    if (Modifier.isStatic(field.getModifiers())) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Autowired annotation is not supported on static fields: " + field);
                        }
                        return;
                    }
                    boolean required = determineRequiredStatus(ann);
                    currElements.add(new AutowiredFieldElement(field, required));
                }
            });
            //...忽略部分代碼
    }

findAutowiredAnnotation()根據(jù)AutowiredProcessor的實(shí)例字段autowiredAnnotationTypes,去查看是否匹配,這個(gè)字段是在AutowiredProcessor創(chuàng)建時(shí)初始化,可以看到支持@Autowired,@Value,@Inject三種類型的注入標(biāo)識(shí).最終據(jù)此完成注入

public AutowiredAnnotationBeanPostProcessor() {
        this.autowiredAnnotationTypes.add(Autowired.class);
        this.autowiredAnnotationTypes.add(Value.class);
        try {
            this.autowiredAnnotationTypes.add((Class&lt;? extends Annotation&gt;)
                    ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
            logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
        }
        catch (ClassNotFoundException ex) {
            // JSR-330 API not available - simply skip.
        }
    }

后記

最后再來梳理一下整個(gè)流程.

首先是AutowiredPorcessor的BeanDefinition的注冊

=> 創(chuàng)建ApplicationContext

? => 創(chuàng)建AnnotatedBeanDefinitionReader

? => 注冊BeanDefinition registerAnnotationConfigProcessors

然后是AutowiredProcessor注冊為bean

=> registerBeanPostProcessors

最后是注入

? => 獲取bean getBean()

? => 創(chuàng)建bean doCreateBean()

? =>生成bean populateBean()

? => 應(yīng)用AutowiredProcessor ibp.postProcessProperties()

? => 找到可注入的字段 buildAutowiringMetadata

? => 注入 metadata.inject

至此,@Autowired生效邏輯梳理完成

到此這篇關(guān)于SpringBoot中@Autowired生效方式詳解的文章就介紹到這了,更多相關(guān)SpringBoot @Autowired內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • J2EE基礎(chǔ)之EJB全面了解

    J2EE基礎(chǔ)之EJB全面了解

    下面小編就為大家?guī)硪黄狫2EE基礎(chǔ)之EJB全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • springboot多文件上傳代碼實(shí)例及解析

    springboot多文件上傳代碼實(shí)例及解析

    這篇文章主要介紹了springboot多文件上傳代碼實(shí)例及解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java ThreadLocal 線程安全問題解決方案

    Java ThreadLocal 線程安全問題解決方案

    這篇文章主要介紹了Java ThreadLocal 線程安全問題解決方案的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Spring Gateway自定義請求參數(shù)封裝的實(shí)現(xiàn)示例

    Spring Gateway自定義請求參數(shù)封裝的實(shí)現(xiàn)示例

    這篇文章主要介紹了Spring Gateway自定義請求參數(shù)封裝的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解SpringBoot接收參數(shù)的五種形式

    詳解SpringBoot接收參數(shù)的五種形式

    在Spring Boot中,接收參數(shù)可以通過多種方式實(shí)現(xiàn),本文給大家介紹了SpringBoot接收參數(shù)的五種形式,并通過代碼和圖文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • elasticsearch啟動(dòng)警告無法鎖定JVM內(nèi)存

    elasticsearch啟動(dòng)警告無法鎖定JVM內(nèi)存

    今天小編就為大家分享一篇關(guān)于elasticsearch啟動(dòng)警告無法鎖定JVM內(nèi)存,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java 正則表達(dá)式詳細(xì)使用

    Java 正則表達(dá)式詳細(xì)使用

    這篇文章主要介紹了Java 正則表達(dá)式詳細(xì)使用,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-10-10
  • java Socket無法完全接收返回內(nèi)容的解決方案

    java Socket無法完全接收返回內(nèi)容的解決方案

    這篇文章主要介紹了java Socket無法完全接收返回內(nèi)容的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式

    struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式

    這篇文章主要介紹了struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式,有需要的朋友可以參考一下
    2014-01-01
  • 使用Feign調(diào)用第三方http接口

    使用Feign調(diào)用第三方http接口

    這篇文章主要介紹了使用Feign調(diào)用第三方http接口,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論