ContextLoaderListener vs DispatcherServlet
In XML based Spring MVC configuration, you must have seen two declarations in web.xml file i.e. ContextLoaderListener and DispatcherServlet. Let’s try to understand their purpose in framework and their differences.
Root and child contexts
Before reading further, please understand that –
- Spring can have multiple contexts at a time. One of them will be root context, and all other contexts will be child contexts.
- All child contexts can access the beans defined in root context; but opposite is not true. Root context cannot access child contexts beans.
DispatcherServlet – Child application contexts
DispatcherServlet is essentially a Servlet (it extends HttpServlet) whose primary purpose is to handle incoming web requests matching the configured URL pattern. It take an incoming URI and find the right combination of controller and view. So it is the front controller.
When you define a DispatcherServlet in spring configuration, you provide an XML file with entries of controller classes, views mappings etc. using contextConfigLocationattribute.
<servlet> <servlet-name>employee-services</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:employee-services-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet> |
If you do not provide configuration file then it will load its own configuration file using [servlet_name]-servlet.xml. Web applications can define any number of DispatcherServlet entries. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc.
It means that each DispatcherServlet has access to web application context. Until specified, each DispatcherServlet creates own internal web application context.
DispatcherServlet(WebApplicationContext webApplicationContext) create a new DispatcherServlet with the given web application context. It is possible only in Servlet 3.x environment through the ServletContext.addServlet(java.lang.String, java.lang.String) API support.ContextLoaderListener – Root application context
ContextLoaderListener creates the root application context and will be shared with child contexts created by all DispatcherServlet contexts. You can have only one entry of this in web.xml.
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value></context-param> |
The context of ContextLoaderListener contains beans that globally visible, like services, repositories, infrastructure beans, etc. After the root application context is created, it’s stored in ServletContext as an attribute, the name is:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);//Where attibute is defined in /org/springframework/web/context/WebApplicationContext.java asWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT"; |
To get root application context in Spring controller, you can use WebApplicationContextUtils class.
@AutowiredServletContext context; ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);if(ac == null){ return "root application context is null";} |
ContextLoaderListener vs DispatcherServlet
Below image describe the whole relation in single view.
ContextLoaderListener vs DispatcherServlet
ContextLoaderListenercreates root application context.DispatcherServletentries create one child application context per servlet entry.- Child contexts can access beans defined in root context.
- Beans in root context cannot access beans in child contexts (directly).
- All contexts are added to
ServletContext. - You can access root context using
WebApplicationContextUtilsclass.
Summary
Generally, you will define all MVC related beans (controller and views etc) in DispatcherServlet context, and all cross-cutting beans such as security, transaction, services etc. at root context by ContextLoaderListener.
Generally, this setup works fine because rarely you will need to access any MVC bean (from child context) into security related class (from root context). Mostly we use security beans on MVC classes, and they can access it with above setup.
Happy Learning !!
https://howtodoinjava.com/spring-mvc/contextloaderlistener-vs-dispatcherservlet/
ContextLoaderListener vs DispatcherServlet的更多相关文章
- 【转】ContextLoaderListener和DispatcherServlet加载内容的区别
一.ContextLoaderListener加载内容 二.DispatcherServlet加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启 ...
- web.xml中的ContextLoaderListener和DispatcherServlet区别
ContextLoaderListener和DispatcherServlet都会在Web容器启动的时候加载一下bean配置. 区别在于: DispatcherServlet一般会加载MVC相关的be ...
- 使用shiro 框架 报错No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?
1.问题描述:ssm 框架中使用shiro 中出现问题 原来web.xml 文件如下: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, ...
- 【转】ContextLoaderListener 和 DispatcherServlet
转载地址: http://www.guoweiwei.com/archives/797 DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供S ...
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件 ...
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
http://www.lai18.com/content/9755931.html Spring 容器(Spring 的上下文) https://my.oschina.net/jast90/blog/ ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...
- 【Spring】浅谈ContextLoaderListener及其上下文与DispatcherServlet的区别
一般在使用SpingMVC开发的项目中,一般都会在web.xml文件中配置ContextLoaderListener监听器,如下: <listener> <listener-clas ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】
原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...
随机推荐
- 51nod 1040:最大公约数之和(数论)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 给出一个n,求1-n这n个数,同n的最大公约数的和. ...
- webpack 图片文件处理loader
目录结构: 引用图片: body { /*background: red;*/ /*background: url("../img/test2.jpg"); 小图片*/ backg ...
- 回炉Spring--事务及Spring源码
声明式事务 配置文件信息: /** * @EnableTransactionManagement 开启基于注解的事务管理功能 * 1.配置数据源 * 2.配置事务管理器来管理事务 * 3.给方法上标注 ...
- Mac上VMWare Fusion配置多台cent os
一.创建虚拟机(准备工作) 1.使用VMWare Fusion 创建第一台虚拟机 2.选择操作系统(本次使用的是使用cent os 6.5 64bit 系统) 3.选择磁盘大小(楼主mac上的磁盘大小 ...
- 【玩转SpringBoot】异步任务执行与其线程池配置
同步代码写起来简单,但就是怕遇到耗时操作,会影响效率和吞吐量. 此时异步代码才是王者,但涉及多线程和线程池,以及异步结果的获取,写起来颇为麻烦. 不过在遇到SpringBoot异步任务时,这个问题就不 ...
- 抽象类 抽象方法 abstract
abstract: * abstract修饰类: 抽象类 * > 此类不能被实例化 * > 抽象类中一定要有构造器, 便于子类对象实例时调用(涉及子类对象实例化过程) * > 开发中 ...
- delphi xe2 panel 无法 遮盖 label 的 解决方案。
百度 没有找到此答案. 问群,也没答案. 根据群友的提示. 找到的解决的办法. 很简单. ParentBackground 设置为 false.
- left、pixelLeft、posLeft的区别
yexj00.style.pixelLeft=50yexj00.style.left=50pxyexj00.style.posLeft=50he.style.pixelLeft=39he.style. ...
- Java + selenium 元素定位(4)之By CSS
这篇我要介绍元素定位的倒数第二个方法啦,就是基于CSS的元素定位.关于一些CSS的知识,我这里就不累赘的讲了,以后可能会单独写一篇关于CSS的介绍.当然个人推荐如果之前完全没有CSS只是储备的,可以选 ...
- 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d解决方法
安装HP LoadRunner 11.00 未找到源文件:C:\loadrunner-11\urunner MSI\bin\icudt36.dll.o1d 下载loadrunner11 使用迅雷下载, ...