写了那么久的Spring,经常写这样的配置,这就是几行Spring、SpringMvc的基本配置, 但是最近也看到不写最前面的context-param以及listener的,好奇记录下.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>Springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>Springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

ContextLoaderListener监听器介绍

上图是ContextLoaderListener的结构图以及javadoc.    ContextLoaderListener肯定要实现ServletContextListener,这点不用多说,另外继承的类是ContextLoader.

JavaDoc告诉我们很多信息:Bootstrap listener启动类监听器用来启动/停止Spring Web上下文容器,代理给ContextLoader来完成启动启动工作。 listener应当在Log4jConfigListener之后注册。

Spring3.1支持通过ContextLoaderListener构造器方式注入Web 上下文容器,servlet3.0也支持使用WebApplicationInitializer启动Spring容器来替代web.xml写法。

主要关注的是ContextLoaderListener,先主要关注容器启动这块吧!监听器的contextInitialized 方法:调用了ContextLoader的initWebApplicationContext,还是代理给了ContextLoader完成容器启动工作。

代码太占篇幅了,记录下加载过程即可:

step1.检查ServletContext上下文是否有WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个属性,没有是正常的,有的话就说明Spring根容器重复定义了!

step2. 尝试读取context-param中为contextClass的值作为根容器的class属性,如果没有指定context-param属性,就以ContextLoader类所在同一路径下的ContextLoader.properties文件中的 org.springframework.web.context.WebApplicationContext 作为key取出value , 默认为 org.springframework.web.context.support.XmlWebApplicationContext , 以这个作为Spring根容器类型,并且实例化XmlWebApplicationContext;

step3. Spring根容器的ServletContext设置为当前ServletContext,并且读取web.xml中 其contextConfigLocation作为 Spring配置文件位置,最后调用 根容器的refresh 方法完成容器启动!

step4. 根容器启动完之后,设置到 ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 属性中 ,并且存到了ContextLoader中,可以通过静态方法ContextLoader.getCurrentWebApplicationContext就能获取到 Spring根容器。

DispatcherServlet的初始化

初始化的入口位于 org.springframework.web.servlet.HttpServletBean#init :

SpringMvc容器类型默认为XmlWebApplicationContext,反射实例化该对象,并且通过ServletContext的 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性得到Spring根容器,

作为SpringMVC容器的父容器。  另外,像如下形式的SpringMvc,没有指定spring配置文件的位置,那默认加载的配置文件位置为: /WEB-INF/Springmvc-servlet.xml

<servlet>
<servlet-name>Springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>Springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

也就是说没有ContextLoaderListener,对于SpringMvc容器的影响就是没了父容器,照样可以使用SpringMVC的特性

获取Spring根容器以及Web容器的方式

提供五种获取Spring根容器方案、三种获取Spring父容器方案:

@Controller
@RequestMapping("/context")
public class ContextController implements ApplicationContextAware { @Autowired
private ApplicationContext ac; @RequestMapping("/demo1")
@ResponseBody
public String demo1(HttpServletRequest request){
System.out.println("Spring根容器方式一:"+request.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
System.out.println("Spring根容器方式二:"+ContextLoader.getCurrentWebApplicationContext());
System.out.println("Spring根容器方式三:"+ WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()));
System.out.println("Spring根容器方式四:"+ WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()));
System.out.println("Spring根容器方式五:"+ac.getParent());
System.out.println("SpringMvc容器获取方式一:"+ac);
System.out.println("SpringMvc容器获取方式二:"+ac2);
//SpringMvc容器获取方式三 继承抽象类 WebApplicationObjectSupport,方式二方式三不能同时用
//调用getApplicationContext
return "hello World";
} private ApplicationContext ac2;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ac2=applicationContext;
}
}

ContextLoaderListener可以不写嘛?的更多相关文章

  1. 新手SSH基础框架搭建

    SSH 为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架. 首先我们先了解SSH的框架所需的包和基本概念: 一.下面我们先来了解一下strut ...

  2. 时隔两个月再写的Echarts(Enterprise Charts,商业级数据图表)一文

    简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...

  3. 写了好多次SSH现在对于框架还是有一定的基础了,但是对于框架下我们该如何进行操作呢???

    首先,对于一个老手来说,我们最快捷的就是ctrl+c和ctrl+v,但是我们自己应该复制哪一些代码呢? 1.在我们导完包之后,我们需要写的就是web.xml,在其中,我们要有过滤器及映射和监听器. w ...

  4. javaweb写的在线聊天应用

    写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...

  5. contextloaderlistener

    http://blog.csdn.net/c5153000/article/details/6234207 作用:在启动Web容器时,自动装配Spring applicationContext.xml ...

  6. org.springframework.web.context.ContextLoaderListener(转载)

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  7. 【转载】Spring中DispatcherServlet与ContextLoaderListener的区别

    昨天在写springmvc的时候,在web.xml中配置了DispatcherServlet,如下: <servlet> <servlet-name>DispatcherSer ...

  8. ContextLoaderListener作用详解(转)

    ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...

  9. ContextLoaderListener作用详解

    参考网址:http://blog.csdn.net/ysughw/article/details/8992322 ContextLoaderListener监听器的作用就是启动Web容器时,自动装配A ...

随机推荐

  1. Node.js web发布到AWS ubuntu 之后,关闭Putty,Node 项目也随之关闭的解决办法

    最近公司把BlockChain和对应的Node Web都发布到了AWS 的ubuntu 系统上. 但是遇到了一个问题,每次启动 Node Web之后,关闭Putty,Node Web也随之关闭. 由于 ...

  2. 受欢迎的牛[HAOI2006]

    --BZOJ1051 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 ​ 种关系是具有传递性的,如果A认为B受欢迎, ...

  3. goaccess

    找了各种工具,最终还是觉得goaccess不仅图文并茂,而且速度快,每秒8W 的日志记录解析速度,websocket10秒刷新统计数据,站在巨人肩膀上你也会看得更远…先上图:  具体方案如下步骤: 一 ...

  4. bittorrent 学习(一) 种子文件分析与bitmap位图

    终于抽出时间来进行 BITTORRENT的学习了 BT想必大家都很熟悉了,是一种文件分发协议.每个下载者在下载的同时也在向其他下载者分享文件. 相对于FTP HTTP协议,BT并不是从某一个或者几个指 ...

  5. 创建.NET core的守护进程

    http://www.cnblogs.com/savorboard/p/dotnetcore-supervisor.html

  6. 【机器学习】Octave 实现逻辑回归 Logistic Regression

    ex2data1.txt ex2data2.txt 本次算法的背景是,假如你是一个大学的管理者,你需要根据学生之前的成绩(两门科目)来预测该学生是否能进入该大学. 根据题意,我们不难分辨出这是一种二分 ...

  7. 339. Nested List Weight Sum

    https://leetcode.com/problems/nested-list-weight-sum/description/ Given a nested list of integers, r ...

  8. 引用数据类型 Scanner和 Random

    键盘录入Scanner 获取键盘录入的数据,对获取数据的具体操作进行了封装,只需要调用方法,即可得到键盘录入的数据 A:导包            import java.util.Scanner;  ...

  9. break语句和continue语句

    1. break 语句 break语句只能用在switch语句中,其作用是跳出switch语句或跳出本层循环. 2. continue 语句 continue语句只能用在循环体中,用于结束本次循环,即 ...

  10. lucene的suggest(搜索提示功能的实现)

    1.首先引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-suggest --> <!-- ...