`

spring源码学习系列2-容器初始化入口-refresh

 
阅读更多
context=XmlWebApplicationContext

org.springframework.web.context.support
Class XmlWebApplicationContext

java.lang.Object
org.springframework.core.io.DefaultResourceLoader
org.springframework.context.support.AbstractApplicationContext
org.springframework.context.support.AbstractRefreshableApplicationContext
org.springframework.context.support.AbstractRefreshableConfigApplicationContext
org.springframework.web.context.support.AbstractRefreshableWebApplicationContext
org.springframework.web.context.support.XmlWebApplicationContext
All Implemented Interfaces:
Closeable, AutoCloseable, Aware, BeanFactory, BeanNameAware, DisposableBean, HierarchicalBeanFactory, InitializingBean, ListableBeanFactory, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, EnvironmentCapable, ResourceLoader, ResourcePatternResolver, ThemeSource, ConfigurableWebApplicationContext, WebApplicationContext


abstractApplicationContext#refresh:
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
//这里主要是标示spring为激活状态及开始时间,初始化环境部分在进入refresh()方法之前,大部分已完成
//初始化context的环境environment(environment抽象表示spring环境)
//主要有systemEnvironment,systemProperties,servletContext,servletConfig,jndi环境等
//对应于web.xml配置的<context-param><init-param>等标签
//环境实际类为StandardServletEnvironment
//environment抽象环境的api从spring3.1开始加入
			// Prepare this context for refreshing.
			prepareRefresh();

//注册容器DefaultListableBeanFactory.beanDefinitionMap
//<spring源码学习系列2.1-从xml到document>
//<spring源码学习系列2.2-从document到beanDefinition>
			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

//beanFactory设置属性,添加后处理器,添加自动装配忽略的接口,自动装配指定值,注册单例(如:environment)

//beanFactory自身是不是在singletonObjects里?
			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
//beanFactory添加后置处理器,添加自动装配忽略的接口,注册bean范围,为自动装配指定值,注册单例(如:servletContext,servletConfig,contextAttributes)

//跟prepareBeanFactory比较像,对beanFactory进行填充。不过此方法根据英文注释子类覆盖用的,自定义的子类可以覆盖改方法进行一些操作,而上面的prepareBeanFactory是比较通用的。在此环境下是AbstractRefreshableWebApplicationContext重写了postProcessBeanFactory
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

//实例化BeanFactoryPostProcessor(getBean)并调用
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

//实例化BeanPostProcessor(getBean)并注册beanPostProcessors
				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

//实例化messageSource(getBean),messageSource这里是context的一个属性
				// Initialize message source for this context.
				initMessageSource();

//实例化applicationEventMulticaster(getBean),applicationEventMulticaster这里是context的一个属性
				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

//提供了子类实例化其他特殊类,如themeSource。配合<spring:theme>标签使用
				// Initialize other special beans in specific context subclasses.
				onRefresh();

//实例化并注册监听器
//spring的事件也实现了javaapi-EventObject
//为什么要放在这个位置,而不是initApplicationEventMulticaster()的下面?它们的关联性更大
				// Check for listener beans and register them.
				registerListeners();

//实例化其他非懒加载的单例bean
//<spring源码学习系列2.3-从beanDefinition到instance>
				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

//实例化lifecycleProcessor,并回调smartLifeCycle.onRefresh()
//发布刷新完成事件
//注册MBeanServer-JConsole looks at the beans on that server
				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

//清除缓存,回调DisposableBean.destory()
				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}
		}
	}


要了解这些方法要做什么,除了代码,最好的方式就是看类或方法上的英文注释

为什么父类中方法互相调用,如果子类重写了会调用子类的方法?


spring环境的抽象-environment

spring为什么既实现了某些接口,又将其作为属性?
spring只是对beanFactory messageSource的封装,表明其有怎样的功能及特性。spring还是调用beanFactory或者messageSource等实现其功能


一般直接在eclipse中立即关闭tomcat有什么影响,会不会有些正在消费的消息未被处理?


参考:
https://yd.baidu.com/view/dc2aa92a9ec3d5bbfc0a741e?cn=6-144,6-708&pn=3

http://blog.csdn.net/szwandcj/article/details/50762990

Spring3.1新属性管理API:PropertySource、Environment、Profile
http://jinnianshilongnian.iteye.com/blog/2000183

Spring中Bean初始化实例【重要】
http://uule.iteye.com/blog/2094609

Spring8:一些常用的Spring Bean扩展接口
http://www.cnblogs.com/xrq730/p/5721366.html

【Spring学习25】容器级启动和关闭回调
http://blog.csdn.net/soonfly/article/details/69916806

5. IoC 容器
http://spring.cndocs.tk/beans.html

http://stackoverflow.com/questions/3924727/managementfactory-getplatformmbeanserver-vs-mbeanserverfactory-creatembeanserv

http://stackoverflow.com/questions/13551408/how-does-lifecycle-interface-work-in-spring-what-are-top-level-singleton-beans
分享到:
评论

相关推荐

    阿里P7美团T8大咖带你学习Spring5源码 视频教程 下载 百度网盘链接2.zip

    23 Spring源码阅读 refresh-invokeBeanFactoryPostProcessor-2.mp4 24 Srping源码阅读refresh-invokeBeanFacotryPostProcessor-3.mp4 25 Spring源码阅读refresh-registerBeanPostProcessor.mp4 26 Spring源码阅读...

    阿里P7美团T8大咖带你学习Spring5源码 视频教程 下载 百度网盘链接3.zip

    23 Spring源码阅读 refresh-invokeBeanFactoryPostProcessor-2.mp4 24 Srping源码阅读refresh-invokeBeanFacotryPostProcessor-3.mp4 25 Spring源码阅读refresh-registerBeanPostProcessor.mp4 26 Spring源码阅读...

    阿里P7美团T8大咖带你学习Spring5源码 视频教程 下载 百度网盘链接4.zip

    23 Spring源码阅读 refresh-invokeBeanFactoryPostProcessor-2.mp4 24 Srping源码阅读refresh-invokeBeanFacotryPostProcessor-3.mp4 25 Spring源码阅读refresh-registerBeanPostProcessor.mp4 26 Spring源码阅读...

    阿里P7美团T8大咖带你学习Spring5源码 视频教程 下载 百度网盘链接1.zip

    23 Spring源码阅读 refresh-invokeBeanFactoryPostProcessor-2.mp4 24 Srping源码阅读refresh-invokeBeanFacotryPostProcessor-3.mp4 25 Spring源码阅读refresh-registerBeanPostProcessor.mp4 26 Spring源码阅读...

    Spring源码refresh方法调试笔记

    `refresh` 方法是Spring框架中的一个核心方法,它主要用于初始化整个应用上下文(ApplicationContext),包括加载配置文件、创建Bean实例等重要步骤。本文将深入剖析Spring框架中`refresh`方法的具体实现流程,以及...

    官方源码 spring-framework-5.3.3.zip

    通过研读Spring Framework 5.3.3的源码,开发者可以深入理解IoC和AOP的设计模式,学习如何构建高度模块化和可扩展的系统,同时也能掌握Web开发的最佳实践,提升自身编程和架构设计能力。 总结,Spring Framework ...

    spring-cloud-examples

    spring-cloud-config-svn-refresh:配置中心svn版本示例,客户端refresh版本示例 spring-cloud-config-eureka:配置中心服务化和高可用代码示例 spring-cloud-config-eureka-bus:配置中心和消息总线示例(配置中心...

    Ultra-Pull-To-Refresh上拉加载的使用事例

    在Android应用开发中,...3. 初始化和设置:在Activity或Fragment的onCreate()方法中,找到布局中的UltraPullToRefreshLayout并进行初始化,设置相应的回调监听器,以便在用户执行下拉刷新或上拉加载时调用。 ```java ...

    Spring Bean创建初始化流程.docx

    在Spring框架中,Bean的创建和初始化是IoC(Inversion of Control)容器的核心功能,这一过程涉及到多个步骤。以下是对Spring Bean创建初始化流程的详细解释: 1. **初始化ApplicationContext**: 开始时,通过`...

    spring-security-oauth2-authorization-server.zip

    本项目“spring-security-oauth2-authorization-server”将带你深入理解如何利用Spring Security OAuth2构建一个授权服务器,以保护你的API并提供安全的访问控制。 OAuth2是一种开放标准,用于授权第三方应用访问...

    Spring源码总结.pdf

    - `obtainFreshBeanFactory()`创建并初始化BeanDefinition对象,这是Spring容器的核心。 - 创建`XmlBeanDefinitionReader`读取配置文件。 - 把XML文件解析为`Document`对象。 - `BeanDefinitionDocumentReader`...

    spring-boot-db-initialization-test:测试用例,显示未初始化数据库以进行测试的错误

    spring-boot-db初始化测试 测试用例,显示未初始化数据库以进行测试的错误 该项目包含一个测试,该测试查询内存中的h2数据库以确保其不为空 $ mvn test ... 2014-10-14 10:10:21.409 WARN 3435 --- [ main] o.s.b.a....

    5.2、spring-refresh-obtainFreshBeanFactory-parseDefaultElement加载解析bean.vsdx

    本次为spring解析配置文件中spring.xml的过程,并且根据解析的spring.xml中的节点加载bean到map中。

    Ultra-pull-to-refresh 三方库

    5. 初始化和监听:在Activity或Fragment的onCreate()方法中,找到对应的布局视图,初始化UltraPullToRefreshLayout,并设置刷新监听器。监听器回调方法包括onRefresh(),在其中执行实际的刷新操作。 三、使用示例及...

    iscroll-5-pull-to-refresh-and-infinite-demo

    要使用 iScroll-5-pull-to-refresh-and-infinite 示例,首先需要引入 iScroll 的库文件,然后初始化 iScroll 实例,并设置相应的配置。配置项中,`pullToRefresh` 和 `infinite` 分别控制下拉刷新和无限滚动的启用。...

    mina-pull-down-refresh-master.zip

    "mina-pull-down-refresh-master"提供的方案通过精细化的事件处理和状态管理,实现了两者之间的无缝配合,使得下拉刷新功能能够顺畅、准确地工作,为开发者提供了有价值的参考。在实际应用中,开发者可以根据具体...

    springBean加载过程源码解析文档,附有代码类名和行数

    Spring Bean 加载过程是 Spring 框架中最核心的部分之一,涉及到 ApplicationContext 的初始化、Bean 的加载和注册等过程。在 Spring Boot 应用程序中,SpringApplication 负责加载和管理 Bean。 SpringApplication...

    spring-framework-4.3.12.RELEASE--编译好的源码.rar

    通过跟踪`refresh()`方法,可以了解如何加载和解析配置文件,以及如何初始化Bean。 - `AOP`相关的类,如`AbstractAdvisingBeanPostProcessor`和`ProxyCreator`,揭示了Spring如何通过代理模式实现AOP功能。 通过...

    Spring源码分析_Spring_IOC

    5. **初始化BeanFactory**:调用`BeanFactory`的`refresh()`方法,完成整个IOC容器的初始化过程。这包括Bean定义信息的注册、Bean的实例化、依赖注入、初始化等操作。 #### FileSystemXmlApplicationContext与...

    spring-cloud使用的各种示例

    学习系列: - [springcloud(一):大话Spring Cloud](http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html) - [springcloud(二):注册中心Eureka]...

Global site tag (gtag.js) - Google Analytics