spring容器启动的加载过程(一)
使用spring,我们在web.xml都会配置ContextLoaderListener
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
//由于他继承自ContextLoader,加载的时候当然会先加载他啦,那我们来看看这个类
}
ContextLoader这个类有一段静态代码块
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";//这个文件是在org.springframework.web.context.ContextLoader.properties他里面是
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext //这个后面再研究 以后就会把spring所有重要的东西都能看到了。
private static final Properties defaultStrategies;
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);//根据上面的properties文件,就能得到一个默认的properties了。
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
}
第一步:我们在看看ContextLoaderListener的这个方法,在容器启动的时候,他会扫描web.xml的listener配置,然后自动调用Listener的contextInitialized初始化方法。
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
第二步:initWebApplicationContext初始化web应用上下文
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);//判断是否有WebApplicationContext,没有则创建,创建得到的是ConfigurableWebApplicationContext
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) { //上下文尚未活动
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);//决定根Web应用程序环境是否存在父应用程序环境,一般是返回null
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);//在这里是初始化web应用上下文的入口 ,spring的依赖注入也从这里开始了。
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);//把创建的根Web应用程序环境保存到Servlet环境中,每个派遣器Servlet加载的子环境会应用这个环境作为父环境
ClassLoader ccl = Thread.currentThread().getContextClassLoader();//取得线程的类加载器
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;//如果线程和本类拥有相同的类加载器,则使用静态变量保存即可,因为同一类加载器加载同一份静态变量
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);//如果线程和本类拥有不同的类加载器,则使用线程的类加载器作为键值保存在一个映射对象里,保证析构时能拿到Web应用程序环境进行关闭操作
}
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);//如果产生任何异常,则保存异常对象到Servlet环境里
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);//如果产生任何错误,则保存错误对象到Servlet环境里
throw err;
}
}
第三步:得到具体的应用上下文
public static final String CONTEXT_CLASS_PARAM = "contextClass"; private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties"; private static final Properties defaultStrategies; //这个静态代码块,会去加载相同目录下的ContextLoader.properties,这个目录是org.springframework.web.context,这个配置文件的配置信息就只有org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
}
} protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
Class<?> contextClass = determineContextClass(sc);//决定要得到哪个ContextClass
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); //实例化Web应用程序环境类
} protected Class<?> determineContextClass(ServletContext servletContext) {
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);//获取web.xml配置的contextClass
if (contextClassName != null) {//配置了contextClass,返回具体的Class
try {
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
}
}
else {
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());//在上面的静态代码块中已经加载了XmlWebApplicationContext,返回的是XmlWebApplicationContext的全路径名:org.springframework.web.context.support.XmlWebApplicationContext
try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());//没有配置contextClass,则通过配置文件,返回了XmlWebApplicationContext的class
}
catch (ClassNotFoundException ex) {
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
}
}
}
第四步:用于封装ApplicationContext数据并且初始化所有相关Bean对象。它会从web.xml中读取名为contextConfigLocation的配置,这就是spring xml数据源设置,然后放到ApplicationContext中,最后调用传说中的refresh方法执行所有Java对象的创建。
public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation"; 在wen.xml中配置的位置
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
//设置Web应用程序环境的ID
if (idParam != null) {
wac.setId(idParam);
}
else {
// Generate default id...
//如果 Servlet规范 <= 2.4,则使用web.xml里定义的应用程序名字定义Web应用程序名
if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
// Servlet <= 2.4: resort to name specified in web.xml, if any.
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getServletContextName()));
}
else {
// 如果Servlet规范是 2.5, 则使用配置的ContextPath定义Web应用程序名
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
}
//保存Servlet环境
wac.setServletContext(sc);
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);//这里得到"contextConfigLocation"配置的值:classpath*:spring-config.xml,
if (initParameter != null) {
wac.setConfigLocation(initParameter);//设置保存ConfigLocation,待后面加载的时候,扫描里面的配置
}
customizeContext(sc, wac);//提供子类可互换Web应用程序环境的机会 占位符方法
wac.refresh();//刷新Web应用程序环境以加载Bean定义,这里才是把我们XML里定义的bean放入容器的时候
}
"contextConfigLocation"在web.xml的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring-config.xml,
</param-value>
</context-param>
第五步:bean的注入
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();//告诉子类刷新内部bean工厂。
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);//准备bean工厂给上下文使用
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);//允许在上下文后处理bean工厂的子类
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);//调用处理器工厂注册的bean上下文
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);注册bean拦截器拦截bean的创建
// Initialize message source for this context.
initMessageSource();//给上下文初始化消息来源
// Initialize event multicaster for this context.
initApplicationEventMulticaster();//给上下文初始化事件多播
// Initialize other special beans in specific context subclasses.//在特定上下文初始化其他特殊bean子类
onRefresh();
// Check for listener beans and register them.
registerListeners();//检查监听器bean并注册他们
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);//实例化所有剩余(non-lazy-init)单例对象
// Last step: publish corresponding event.
finishRefresh();//发布相应的事件
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();//销毁已经创建的单例,避免悬挂的资源
// Reset 'active' flag.
cancelRefresh(ex);//重置“活跃”标志
// Propagate exception to caller.
throw ex;
}
}
}
spring容器启动的加载过程(一)的更多相关文章
- spring容器启动的加载过程(三)
第十步: public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { /** * Load bean def ...
- spring容器启动的加载过程(二)
第六步: public abstract class AbstractApplicationContext extends DefaultResourceLoader implements Confi ...
- 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...
- 1. spring5源码 -- Spring整体脉络 IOC加载过程 Bean的生命周期
可以学习到什么? 0. spring整体脉络 1. 描述BeanFactory 2. BeanFactory和ApplicationContext的区别 3. 简述SpringIoC的加载过程 4. ...
- Tomcat源码分析三:Tomcat启动加载过程(一)的源码解析
Tomcat启动加载过程(一)的源码解析 今天,我将分享用源码的方式讲解Tomcat启动的加载过程,关于Tomcat的架构请参阅<Tomcat源码分析二:先看看Tomcat的整体架构>一文 ...
- 深入理解 spring 容器,源码分析加载过程
Spring框架提供了构建Web应用程序的全功能MVC模块,叫Spring MVC,通过Spring Core+Spring MVC即可搭建一套稳定的Java Web项目.本文通过Spring MVC ...
- spring启动component-scan类扫描加载过程(转)
文章转自 http://www.it165.net/pro/html/201406/15205.html 有朋友最近问到了 spring 加载类的过程,尤其是基于 annotation 注解的加载过程 ...
- 【Spring源码分析系列】启动component-scan类扫描加载过程
原文地址:http://blog.csdn.net/xieyuooo/article/details/9089441/ 在spring 3.0以上大家都一般会配置一个Servelet,如下所示: &l ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
随机推荐
- ios下点击穿透focus获取问题
在ios下的浏览器中当点击当前页的一个按钮,用window.location.href进行跳转时,如果下一个页面里这点击按钮的位置是一个textarea或者text等那么他会触发focus事件,会出现 ...
- quagga源码分析--通用库command
quagga作为一个路由器软件,自然要提供人机接口. quagga提供snmp管理接口,而且,自然就会有对应的命令行管理格式,当然一般路由软件不会提供界面形式的,也许有webui,然而quagga并没 ...
- MultipartResolver 文件上传
SpringMVC 中文件上传 MultipartResolver 博客分类: SpringMVC - 基础篇 基于前面文章的基础上. 一.准备 需要的jar 二.配置 1. spmvc-se ...
- Python爬虫----Beautiful Soup4 基础
1. Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyth ...
- My网页
开始更新|Version:2.46|更新内容:/=====================================//1.新增秒低价次数//2.优化捉鬼停留过久的问题//3.优化其他任务上的效 ...
- 使用filter方法过滤集合元素
文章转自https://my.oschina.net/nenusoul/blog/658238 Problem 你想要筛选出集合中的一些元素形成一个新的集合,这些元素都是满足你的筛选条件的. Solu ...
- 查看SQL语句执行时间与测试SQL语句性能
查看SQL语句执行时间与测试SQL语句性能 写程序的人,往往需要分析所写的SQL语句是否够优化.是否能提升执行效率,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了 ...
- python之~ 序列化与反序列化
sy1.proto文件 syntax = "proto2"; message stuff { required int32 stuff_ID = ; required ; opti ...
- 【转】Python BeautifulSoup 中文乱码解决方法
这篇文章主要介绍了Python BeautifulSoup中文乱码问题的2种解决方法,需要的朋友可以参考下 解决方法一: 使用python的BeautifulSoup来抓取网页然后输出网页标题,但是输 ...
- 2014最热门、最具争议的10个Java话题
Java 的哪些内容已在2014年死去,Java 的哪些变更又遭到整个Java社区的竭力反对?请随我们一起来回顾在2014年这个多事之秋中Java都发生了哪些变化,以及小伙伴们都在JAXenter热烈 ...