激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - 關于Spring啟動時Context加載源碼分析

關于Spring啟動時Context加載源碼分析

2021-03-22 11:22Nix.Huang JAVA教程

這篇文章通過源碼分析主要給大家介紹了關于Spring啟動時Context加載的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

前言

本文主要給大家介紹了關于Spring啟動時Context加載的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

測試源碼下載test-annotation.zip

有如下的代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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 構造一個上下文對象, 為了演示的方便, 顯示的聲明了DefaultListableBeanFactory和InstantiationStrategy實例。通過applicationContext.getBean()獲取bean的引用,并調用 service.getHelloMessage() 方法。

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

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

為@Configuration類中@Bean標注的方法生成BeanDefinition對象詳細過程如下

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

?
1
2
3
4
5
6
7
8
9
10
11
org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors() {
 ...
 //獲取適用的BeanDefinitionRegistryPostProcessor bean名稱
 String[] postProcessorNames =
  beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
 ...
 //根據beanName獲取PostProcessor, 處理@Configuration標注類的beanName為
 //org.springframework.context.annotation.internalConfigurationAnnotationProcessor
 //實現為org.springframework.context.annotation.ConfigurationClassPostProcessor
 ConfigurationClassPostProcessor postProcessor =beanFactory.getBean(postProcessorNames[0], BeanDefinitionRegistryPostProcessor.class)
}

步驟2、為@Configuration產生ConfigurationClass對象

//使用ConfigurationClassParser解析@Configuration標注的類,

//每一個@Configuration標注的類產生一個ConfigurationClass對象,

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

//@Bean標注的方法使用BeanMethod對象表示

?
1
2
3
4
5
6
7
8
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標注的方法產生BeanDefinition并注入到DefaultListableBeanFactory中

?
1
2
3
4
5
6
7
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);
}

此過程的調用棧:

關于Spring啟動時Context加載源碼分析

根據BeanDefinition生成實例過程的調用棧:

關于Spring啟動時Context加載源碼分析

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.javacoder.cn/?p=1174

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 爱福利视频网 | 亚洲国产精品一区二区久久 | 中文字幕在线日韩 | 国产精品成人一区二区三区电影毛片 | 国产亚洲自拍一区 | 欧美精品久久久久久久久久 | 国产乱淫av片免费网站 | 成人免费一区 | 欧产日产国产精品99 | 午夜精品久久久久久中宇 | 久草免费资源视频 | 国内精品伊人久久 | 毛片视频网址 | 精品国产一区二区久久 | 亚洲性生活免费视频 | 欧美雌雄另类xxxxx | 99久久免费看精品国产一区 | 污视频在线免费播放 | 午夜久久久久 | 欧美成人理论片乱 | 黄视频网址 | 九一免费国产 | 狠狠干91 | 亚洲热线99精品视频 | 日韩在线视频观看免费 | 5xsq在线视频 | 在线观看免费视频麻豆 | 免费在线国产精品 | 亚洲一区二区三区精品在线观看 | 欧美一级做一级爱a做片性 久久久资源网 | 精品在线观看一区 | 国产乱一区二区三区视频 | 爱福利视频 | 女人a级毛片 | 337p日本欧洲亚洲大胆精蜜臀 | 羞羞视频免费网站含羞草 | 视频在线91| 国产精品久久久久久久久久三级 | 亚洲成人免费电影 | 国产羞羞视频在线免费观看 | 天堂成人国产精品一区 |