最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Spring啟動時Context加載源碼分析

 更新時間:2018年01月15日 11:22:02   作者:Nix.Huang  
這篇文章通過源碼分析主要給大家介紹了關(guān)于Spring啟動時Context加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

本文主要給大家介紹了關(guān)于Spring啟動時Context加載的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

測試源碼下載test-annotation.zip

有如下的代碼

@Component
public class HelloWorldService {
 @Value("${name:World}")
 private String name;
 public String getHelloMessage() {
 return "Hello " + this.name;
 }
}

@Configuration
public class BootStrap {
 @Bean
 public static HelloWorldService helloService() {
 return new HelloWorldService();
 }
 public static void main(String[] args) {
 InstantiationStrategy instantiationStrategy = new SimpleInstantiationStrategy();
 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
 beanFactory.setInstantiationStrategy(instantiationStrategy);
 AnnotationConfigApplicationContext applicationContext = 
 new AnnotationConfigApplicationContext(beanFactory);
 applicationContext.register(BootStrap.class);
 applicationContext.refresh();
 HelloWorldService service = applicationContext.getBean(HelloWorldService.class);
 System.out.println(service.getHelloMessage());
 applicationContext.close();
 }
}

HelloWorldService.getHelloMessage方法簡單的返回name的值, BootStrap.main方法中使用AnnotationConfigApplicationContext 構(gòu)造一個上下文對象, 為了演示的方便, 顯示的聲明了DefaultListableBeanFactory和InstantiationStrategy實例。通過applicationContext.getBean()獲取bean的引用,并調(diào)用 service.getHelloMessage() 方法。

上下文的加載主要發(fā)生在applicationContext.register方法和applicationContext.refresh方法中,
applicationContext.register方法的作用是為參數(shù)(使用@Configuration注解的class)生成BeanDefinition 對象并調(diào)用DefaultListableBeanFactory.registerBeanDefinition將BeanDefinition注冊到DefaultListableBeanFactory中。

applicationContext.refresh()的功能要更多,主要功能一的是調(diào)用PostProcessor為@Configuration類中的@Bean標(biāo)注的方法生成對應(yīng)的BeanDefinition對象,并注冊到DefaultListableBeanFactory中,功能二是遍歷DefaultListableBeanFactory中BeanDefinition, 產(chǎn)生真正的對象。

為@Configuration類中@Bean標(biāo)注的方法生成BeanDefinition對象詳細(xì)過程如下

步驟1、找到合適的BeanDefinitionRegistryPostProcessor處理器

org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors() {
 ...
 //獲取適用的BeanDefinitionRegistryPostProcessor bean名稱
 String[] postProcessorNames =
  beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 ...
 //根據(jù)beanName獲取PostProcessor, 處理@Configuration標(biāo)注類的beanName為
 //org.springframework.context.annotation.internalConfigurationAnnotationProcessor 
 //實現(xiàn)為org.springframework.context.annotation.ConfigurationClassPostProcessor
 ConfigurationClassPostProcessor postProcessor =beanFactory.getBean(postProcessorNames[0], BeanDefinitionRegistryPostProcessor.class)
}

步驟2、為@Configuration產(chǎn)生ConfigurationClass對象

//使用ConfigurationClassParser解析@Configuration標(biāo)注的類,

//每一個@Configuration標(biāo)注的類產(chǎn)生一個ConfigurationClass對象,

//ConfigurationClass.getBeanMethods()能獲得該類中所有使用@Bean標(biāo)注的方法,

//@Bean標(biāo)注的方法使用BeanMethod對象表示

org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
 ConfigurationClassParser parser = new ConfigurationClassParser(
 this.metadataReaderFactory, this.problemReporter, this.environment,
 this.resourceLoader, this.componentScanBeanNameGenerator, registry);
 parser.parse(configCandidates);
 parser.validate();
 this.reader.loadBeanDefinitions(parser.getConfigurationClasses());
}

步驟3、@Bean標(biāo)注的方法產(chǎn)生BeanDefinition并注入到DefaultListableBeanFactory中

org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
 ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass);
 beanDef.setBeanClassName(configClass.getMetadata().getClassName());
 beanDef.setFactoryMethodName(metadata.getMethodName());
 //registry 是DefaultListableBeanFactory的實例
 this.registry.registerBeanDefinition(beanName, beanDefToRegister);
}

此過程的調(diào)用棧:

根據(jù)BeanDefinition生成實例過程的調(diào)用棧:

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Java中的StringTokenizer實現(xiàn)字符串切割詳解

    Java中的StringTokenizer實現(xiàn)字符串切割詳解

    這篇文章主要介紹了Java中的StringTokenizer實現(xiàn)字符串切割詳解,java.util工具包提供了字符串切割的工具類StringTokenizer,Spring等常見框架的字符串工具類(如Spring的StringUtils),需要的朋友可以參考下
    2024-01-01
  • Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請求日志

    Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請求日志

    這篇文章主要介紹了Spring Cloud gateway 網(wǎng)關(guān)如何攔截Post請求日志的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java中SSM+Shiro系統(tǒng)登錄驗證碼的實現(xiàn)方法

    Java中SSM+Shiro系統(tǒng)登錄驗證碼的實現(xiàn)方法

    這篇文章主要介紹了 SSM+Shiro系統(tǒng)登錄驗證碼的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • SpringBoot注解@Import原理之關(guān)于ConfigurationClassPostProcessor源碼解析

    SpringBoot注解@Import原理之關(guān)于ConfigurationClassPostProcessor源碼解析

    這篇文章主要介紹了SpringBoot注解@Import原理之關(guān)于ConfigurationClassPostProcessor源碼解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java代碼為例講解堆的性質(zhì)和基本操作以及排序方法

    Java代碼為例講解堆的性質(zhì)和基本操作以及排序方法

    堆數(shù)據(jù)結(jié)構(gòu)可以看作一顆完全二叉樹,因而又被成為二叉堆,這里我們以Java代碼為例講解堆的性質(zhì)和基本操作以及排序方法,需要的朋友可以參考下
    2016-06-06
  • springboot redis分布式鎖代碼實例

    springboot redis分布式鎖代碼實例

    這篇文章主要介紹了springboot redis分布式鎖代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • spring 定時任務(wù)@Scheduled詳解

    spring 定時任務(wù)@Scheduled詳解

    這篇文章主要介紹了spring 定時任務(wù)@Scheduled的相關(guān)資料,文中通過示例代碼介紹的很詳細(xì),相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。
    2017-01-01
  • 將java程序打成jar包在cmd命令行下執(zhí)行的方法

    將java程序打成jar包在cmd命令行下執(zhí)行的方法

    這篇文章主要給大家介紹了關(guān)于將java程序打成jar包在cmd命令行下執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Java中鎖的分類與使用方法

    Java中鎖的分類與使用方法

    這篇文章主要給大家介紹了關(guān)于Java中鎖分類與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 詳解Java多線程編程中CountDownLatch阻塞線程的方法

    詳解Java多線程編程中CountDownLatch阻塞線程的方法

    在Java中和ReadWriteLock.ReadLock一樣,CountDownLatch的本質(zhì)也是一個"共享鎖",這里我們就來詳解Java多線程編程中CountDownLatch阻塞線程的方法:
    2016-07-07

最新評論

新巴尔虎右旗| 岳阳县| 阜城县| 西乡县| 壤塘县| 策勒县| 昔阳县| 同江市| 棋牌| 鹿邑县| 长治县| 额敏县| 沧州市| 兴安县| 芦山县| 樟树市| 高邑县| 九龙县| 思南县| 郁南县| 武功县| 冀州市| 偏关县| 日土县| 峡江县| 定襄县| 洪洞县| 东阿县| 临城县| 遂昌县| 河源市| 尉犁县| 滦南县| 栖霞市| 漳浦县| 温州市| 百色市| 麻栗坡县| 墨脱县| 衡南县| 五寨县|