Spring IOC-ContextLoaderListener
【spring version : 4.1.6.RELEASE】
使用spring的项目中,一般都会在web.xml中配置ContextLoaderListener,它就是spring ioc 的入口
<!-- spring context listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
入口:ContextLoaderListener.contextInitialized(ServletContextEvent)
1。首先,创建一个上下文 WebApplicationContext context。使用默认的策略: XmlWebApplicationContext 的实例作为上下文。
2. 将 XmlWebApplicationContext 强转成 ConfigurableWebApplicationContext ,设置 parent (是null)
2. 调用 ContextLoader.configureAndRefreshWebApplicationContext 刷新上下文
ContextLoader.configureAndRefreshWebApplicationContext 刷新上下文:
1. 将ServletContext设置到 WebApplicationContext
获取web.xml中配置的contextConfigLocation,设置到 WebApplicationContext 中
2. wac.refresh() 刷新上下文
刷新上下文:
AbstractApplicationContext.refresh() 作如下工作:
// Prepare this context for refreshing.
【1】 prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
【2】 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
1. 创建一个 DefaultListableBeanFactory 的实例。
2. 加载bean的定义 loadBeanDefinitions(DefaultListableBeanFactory)
(BeanDefinitionParser)
// Prepare the bean factory for use in this context.
【3】 prepareBeanFactory(beanFactory);
1. 设置beanFactory的classLoader
2. 设置 beanPostProcesser.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
3. 注册spring内部指定的依赖类型和相应的自动注入值。
// Register a special dependency type with corresponding autowired value.
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
所以,我们可以通过autowired的方式拿到ApplicationContext的实例
4. Register default environment beans.
// Allows post-processing of the bean factory in context subclasses.
【4】 postProcessBeanFactory(beanFactory);
beanFactory 后置处理,留给子类扩展,处理 beanFactory ,做类似【3】中的 prepareBeanFactory() 的事情,比如可以在这里添加 beanPostProcesser。
context 的实例是 XmlWebApplicationContext,这个方法最后会调 AbstractRefreshableWebApplicationContext.postProcessBeanFactory(ConfigurableListableBeanFactory)
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 添加ServletContextAwareProcessor,用来处理实现了ServletContextAware接口的bean
beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
beanFactory.ignoreDependencyInterface(ServletContextAware.class);
beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
}
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext) ---> 会去注册如下几个bean,所以在web项目中,我们可以直接使用@Resource来依赖注入这些bean
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
// Invoke factory processors registered as beans in the context.
【5】 invokeBeanFactoryPostProcessors(beanFactory);
执行beanFactory的postProcessor。(注意与beanPostProcessor区别开)
里面会去找到所有的 BeanDefinitionRegistryPostProcessor 去执行 postProcessBeanDefinitionRegistry()。
比如:ConfigurationClassPostProcessor、org.mybatis.spring.mapper.MapperScannerConfigurer
// Register bean processors that intercept bean creation.
【6】 registerBeanPostProcessors(beanFactory);
注册beanPostProcessor,按顺序注册
// Initialize message source for this context.
【7】 initMessageSource();
// Initialize event multicaster for this context.
【8】 initApplicationEventMulticaster();
初始化事件广播
// Initialize other special beans in specific context subclasses.
【9】 onRefresh();
// Check for listener beans and register them.
【10】 registerListeners();
// 注册事件监听器,事件由ApplicationEventMulticaster去广播
// Instantiate all remaining (non-lazy-init) singletons.
【11】 finishBeanFactoryInitialization(beanFactory);
解析 bean 的依赖,完成 bean 实例的初始化。
1. 先创建 bean 的 proxy 。
AbstractAutowireCapableBeanFactory.createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
.....
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
// 【分析】:它会调用所有的 BeanPostProcessors 来对 bean 进行处理postProcessBeforeInitialization、postProcessAfterInitialization
// 如果要生成代理对象的话,可想而知,BeanFactory在注册BeanDefinition时肯定注册了一个和生成代理相关的 BeanPostProcessor
// 而我们在spring配置文件中配置了 <aop:aspectj-autoproxy proxy-target-class="true" /> ,它肯定会对应一个 BeanDefinitionParser 来解析这个标签。
// 由此,我们又找到了 AspectJAutoProxyBeanDefinitionParser.parse(),这个方法就对 AnnotationAwareAspectJAutoProxyCreator 进行了注册,它间接实现了 BeanPostProcessor
Object bean = resolveBeforeInstantiation(beanName, mbd);
if (bean != null) {
return bean;
}
......
2. 如果创建出来了的话,就返回bean的代理,否则就创建非代理的 bean
里面会决定要不要创建 bean 的 proxy。(AbstractAutoProxyCreator.createProxy)
// Last step: publish corresponding event.
//Finish the refresh of this context, invoking the LifecycleProcessor's onRefresh() method and publishing the ContextRefreshedEvent.
【12】 finishRefresh();
推荐文章:https://www.jianshu.com/p/1dec08d290c1
Spring IOC-ContextLoaderListener的更多相关文章
- Spring学习进阶(二)Spring IoC
在使用Spring所提供的各种丰富而神奇的功能之前,必须在Spring IoC容器中装配好Bean,并建立Bean与Bean之间的关联关系.控制反转(Inverser of Control ioc)是 ...
- 一、Spring——IoC
IOC概述 Spring中IOC的概念,控制反转概念其实包含两个层面的意思,"控制"是接口实现类的选择控制权:而"反转"是指这种选择控制权从调用者转移到外部第三 ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring框架学习02——Spring IOC 详解
1.Spring IOC的基本概念 IOC(Inverse of Control)反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理.当某个Java对象(调用者)需要调用 ...
- Spring IOC 容器源码分析
声明!非原创,本文出处 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 S ...
- 【转】Spring学习---Spring IoC容器的核心原理
[原文] Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的生态帝国. IoC和DI的基本概念 IoC(控制反转,英文含义:Inverse of Control)是Spr ...
- Spring IOC 源码分析
Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 Spring 呢?阅读本文 ...
- Spring—Ioc
IoC容器,最主要的就是完成对象的创建以及维护对象的依赖关系等. 所谓控制反转,包括两部分:一是控制,二是反转,就是把传统方式需要由代码来实现对象的创建.维护对象的依赖关系,反转给容器来帮忙管理和实现 ...
随机推荐
- Java编程的逻辑 (48) - 剖析ArrayDeque
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- 淘宝开放平台获取沙箱token
沙箱环境的文档都是错的,直接使用以下地址: 输入淘宝测试账号: sandbox_c_1 密码: taobao1234 https://login.tbsandbox.com/member/log ...
- IIS连接数修改
1, 对于xp等非服务器操作系统,IIS的并发数量是有限的.在xp上同时允许的连接数是10. 当程序出现性能问题时,可以通过下面的方法来查看并发连接数 这个工具在计算机管理里面可以找到 在图表区域,右 ...
- CSS(八):定位属性
一.position属性 1.relative(相对定位) 相对它原来的位置,通过指定偏移,到达新的位置. 扔在标准流中,它对父级盒子和相邻的盒子都没有任何影响. 看下面的例子: <!DOCTY ...
- 重装MAC系统 “安装器有效负载签名检查失败” 解决方法
部分朋友反应安装macOS Sierra的时候会提示:"安装器有效负载签名检查失败" 其实这是系统时间不对的原因,把系统时间修改正确就好了. 1,如果电脑正常运行,那么进系统偏好设 ...
- 【转】Memcached之缓存雪崩,缓存穿透,缓存预热,缓存算法
缓存雪崩 缓存雪崩可能是因为数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 解决思路: 1,采用加锁计数,或者使用合理的队列 ...
- loadrunner 性能测试报error-27796的解决
网上观点: 在注册表HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters里,有如下两个键值:TcpTimedWai ...
- 无法加载 Parallels 驱动器
解决 最新版 mac 系统 无法使用未签名第三驱动 10.12.多 我的情况是 10.11.4 Beta (15E27e) 使用绿联usb网卡不正常. 下面的命令为检测驱动是否装载的一些命令.sudo ...
- USB学习笔记连载(十六):USB数字签名
转载:http://blog.chinaaet.com/crazybingo/p/34487 曾记得在最开始安装驱动程序的时候出现过这个错误....但是最近我在别的电脑安装的时候又不出现这个错误了.. ...
- 【转】【Python】Python网络编程
Socket简介 在网络上的两个程序通过一个双向的通信连接实现数据的交换,这个链接的一端称为一个Socket(套接字),用于描述IP地址和端口. 建立网络通信连接至少要一对端口号(Socket),So ...