一、版本
springboot-2.1.3 (spring-5.1.5)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
二、IOC容器初始化的主要代码
IOC容器初始化的核心逻辑是各种ApplicationContext的refresh()方法实现的。
/* 使用模板方法模式定义的一个顶级抽象父类,方法具体实现延迟到子类 */ /* org.springframework.context.support.AbstactApplicationContext */ public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // 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(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset ‘active‘ flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring‘s core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
选取springboot默认启动的AnnotationConfigServletWebServerApplicationContext研究refresh()的实现逻辑。
三、AnnotationConfigServletWebServerApplicationContext
① 加锁
新技能get:由synchronized实现的一个细粒度锁。拥有ReentranLock.lock()的细粒度,还不用释放unlock()。
public void refresh() throws BeansException, IllegalStateException { //private final Object startupShutdownMonitor; //构造方法中 this.startupShutdownMonitor = new Object(); //定义的一个当前applicationContext实例的独占锁 Object var1 = this.startupShutdownMonitor; synchronized(this.startupShutdownMonitor) { ... } }
② this.prepareRefresh():
③ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
④this.prepareBeanFactory(beanFactory);
⑤this.postProcessBeanFactory(beanFactory);
⑥this.invokeBeanFactoryPostProcessors(beanFactory);
⑦this.registerBeanPostProcessors(beanFactory);
⑧this.initMessageSource();
⑨this.initApplicationEventMulticaster();
⑩this.onRefresh();
?this.registerListeners();
?this.finishBeanFactoryInitialization(beanFactory);
?this.finishRefresh();
?this.destroyBeans();
?this.cancelRefresh(var9);
?this.resetCommonCaches();
原文:https://www.cnblogs.com/wqff-biubiu/p/12364643.html