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

猜你喜欢

转载自newjava-sina-cn.iteye.com/blog/2369741
今日推荐