国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?

這篇具有很好參考價值的文章主要介紹了SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

@Value java doc文檔指出,它是由AutowiredAnnotationBeanPostProcessor這個BeanPostProcessor處理的。
AutowiredAnnotationBeanPostProcessor的構(gòu)造方法如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
可見AutowiredAnnotationBeanPostProcessor用來處理@Autowired和@Value這兩個注解。
具體的處理流程是通過在容器對bean進行實例化的時候應(yīng)用上述BeanPostProcessor.

示例分析(以@Value為例):
controller類:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
啟動類:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端

完整流程如下:
主類運行main方法,運行到SpringApplication的靜態(tài)run方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
該方法調(diào)用了重載的run方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
該方法首先創(chuàng)建出了一個SpringApplication對象,然后調(diào)用了該對象的run方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
該run方法中調(diào)用了refreshContex方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
refreshContext方法調(diào)用了refresh方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
該方法又調(diào)用了applicationContext對象的refresh方法,此處的applicationContext的實際類型是ServletWebServerApplicationContext,所以調(diào)用的是ServletWebServerApplicationContext的refresh方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
這個refresh方法調(diào)用了父類的refresh方法,也就是AbstractApplicationContext的refresh方法:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中會調(diào)用finisheBeanFactoryInitialization方法,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中會調(diào)用preInstantiateSingletons方法,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
preInstantiateSingletons這個方法會實例化所有的singleton的bean,
在實例化我自己定義的DemoController類的這個bean時,

其中調(diào)用的getBean方法代碼如下(位于AbstractBeanFactory類):
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用的doGetBean方法代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
然后會執(zhí)行到上圖中畫線的getSingleton,因為此時demoController這個bean還不存在,所以,getSingleton執(zhí)行過成中會調(diào)用lambda表達式中的createBean來創(chuàng)建democontroller bean, createBean的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用了doCreateBean,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端

其中調(diào)用了populateBean,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中對容器中的BeanPostProcessor(AutowiredAnnotationBeanPostProcessor包含在其中)進行遍歷,調(diào)用了postProcessProperties方法,AutowiredAnnotationBeanPostProcessor的postProcessProperties代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用的inject方法代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端

其中調(diào)用的inject方法代碼如下(位于AutowiredAnnotationBeanPostProcessor):
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
可以看到這個方法首先拿到了要注入的值(value = resolveFieldValue(field, bean, beanName),然后利用反射給bean的屬性設(shè)置了值(field.set(bean,value)).
resolveFiledValue的邏輯是什么呢?代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用了beanFactory.resolveDependency方法,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用了doResolveDependency,它的代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
其中調(diào)用的resolveEmbededValue代碼如下:
SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?,SpringBoot,spring boot,java,后端
在其中會遍歷embeddedValueResolvers成員變量,用它所包含的StringValueResolver進行解析,最后把解析的結(jié)果返回。文章來源地址http://www.zghlxwxcb.cn/news/detail-641329.html

到了這里,關(guān)于SpringBoot復(fù)習:(18)@Value和@Autowired注解配置的屬性是怎么注入到bean中的?的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 關(guān)于在 springboot 中使用 @Autowired 注解來對 TemplateEngine 進行自動裝配時,無法注入的問題。

    關(guān)于在 springboot 中使用 @Autowired 注解來對 TemplateEngine 進行自動裝配時,無法注入的問題。

    本文是基于江南一點雨的 Spring Boot+Vue 系列視頻教程第 三 章的第三節(jié),詳情參考Spring Boot+Vue系列視頻教程 在觀看學(xué)習這一節(jié)時,發(fā)現(xiàn)當進行手動渲染 Thymeleaf 模板時,通過 @Autowired 注解來對 TemplateEngine 進行自動裝配時,發(fā)現(xiàn) idea 對其顯示 No beans of \\\'TemplateEngine \\\' type of found。

    2024年02月02日
    瀏覽(30)
  • 如何在SpringBoot中使用@Value注解來設(shè)置默認值

    ??????在Spring Boot中,使用@Value注解可以為屬性設(shè)置默認值。 @Value注解可以用于注入以下類型的屬性: 1、基本數(shù)據(jù)類型(如字符串、整數(shù)、浮點數(shù)等) 2、基本數(shù)據(jù)類型的數(shù)組 3、集合類型(如List、Set等) 4、自定義數(shù)據(jù)類型,包括自定義對象和自定義對象的集合 5、Spri

    2024年02月11日
    瀏覽(14)
  • SpringBoot復(fù)習:(7)@Profile注解

    其中,student這個bean只有指定了profile才會裝配,比如在application.properties中: @Profile注解不僅可以用在@Bean上,還可以用在注解類上,用在注解類時 只有設(shè)定了profile為指定的值,該配置類中的所有bean才會裝配

    2024年02月15日
    瀏覽(16)
  • SpringBoot復(fù)習(30):@DateTimeFormat注解的使用

    一、實體類 二、控制器類:

    2024年02月13日
    瀏覽(21)
  • Spring Boot項目怎么對System.setProperty(key, value)設(shè)置的屬性進行讀取加解密

    Spring Boot項目怎么對System.setProperty(key, value)設(shè)置的屬性進行讀取加解密

    之前我寫過一篇文章使用SM4國密加密算法對Spring Boot項目數(shù)據(jù)庫連接信息以及yaml文件配置屬性進行加密配置(讀取時自動解密),對Spring Boot項目的屬性讀取時進行加解密,但是沒有說明對System.setProperty(key, value)設(shè)置的屬性進行讀取加解密,這個在開發(fā)過程中應(yīng)該怎么實現(xiàn)呢

    2024年02月22日
    瀏覽(21)
  • SpringBoot復(fù)習:(34)@EnableWebMvc注解為什么讓@WebMvcAutoconfiguration失效?

    SpringBoot復(fù)習:(34)@EnableWebMvc注解為什么讓@WebMvcAutoconfiguration失效?

    它導(dǎo)入了DelegatingWebMvcConfiguration 它會把容器中的類型為WebMvcConfigurer的bean注入到類型為WebMvcConfigurerComposite的成員變量configurers中。 可以看到它繼承了WebMvcConfigurerSupport類 而WebMvcConfigureAutoConfiguration類定義如下 可以看到一個@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)注解。 所

    2024年02月13日
    瀏覽(43)
  • Spring @Autowired 注解原理

    Spring @Autowired 注解原理

    被掃描的組件配置類 輸出結(jié)果 定位@Autowired所在包 org.springframework.beans.factory.annotation.Autowired 找到同包下 AutowiredAnnotationBeanPostProcessor AutowiredAnnotationBeanPostProcessor 的類繼承圖如下 AutowiredAnnotationBeanPostProcessor實現(xiàn)了InstantiationAwareBeanPostProcessor與 MergedBeanDefinitionPostProcessor兩個Bea

    2024年02月16日
    瀏覽(33)
  • autowired和resource注解的區(qū)別

    注入規(guī)則不同 Autowired注解是spring的注解,此注解只根據(jù)type進行注入,不會去匹配name.但是如果只根據(jù)type無法辨別注入對象時,就需要配合使用@Qualifier注解或者@Primary注解使用。 Resource注解有兩個重要的屬性,分別是name和type,如果name屬性有值,則使用byName的自動注入策略,將值

    2024年02月10日
    瀏覽(14)
  • Spring屬性注解對配置項名稱的自動轉(zhuǎn)換

    ???????在Spring中,我們經(jīng)常需要將配置文件中的屬性值注入到Java類中。Spring提供了兩個主要的注解來實現(xiàn)這一功能: @Value 和 @ConfigurationProperties 。其中 @ConfigurationProperties 支持將配置項名稱與Java類中的屬性名進行自動轉(zhuǎn)換,包括 \\\' - \\\'?和\\\' 駝峰命名 \\\'的轉(zhuǎn)換;而 @Value 不支

    2024年02月15日
    瀏覽(18)
  • @Autowired注解以及失效幾個原因

    @Autowired注解以及失效幾個原因

    1、Autowired注解作用 使構(gòu)造函數(shù)、字段、設(shè)值方法或配置方法可以被Spring依賴注入工具自動裝配(Autowired)。 用于字段:字段在bean構(gòu)造之后,任何配置方法被調(diào)用之前被注入。被注入的類需要是一個組件(@Component)。該注解不要求字段是public。 在構(gòu)造 bean 之后,在調(diào)用任何

    2023年04月09日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包