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都会 ...
随机推荐
- 【leetcode】900. RLE Iterator
题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...
- Android 获取手机(ios,android)的设备唯一码(mac地址, IMEI)
{ /*获取手机(ios,android)的设备唯一码(mac地址, IMEI)2018年02月16日 ⁄ 综合 ⁄ 共 2697字 ⁄ 字号 小 中 大 ⁄ 评论关闭 app中总会用到客户端下载量数 ...
- Vue的跨域设置
1.在使用vue开发的时候经常要涉及到跨域的问题,其实在vue cli中是有我们设置跨域请求的文件的. 2.当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev:{} ...
- 七、单例设计模式共享数据分析、解决、call_once
一.设计模式大概谈 代码的一些写法,与常规的写法不太一样,程序灵活,维护起来很方便,但是别人接管.阅读代码很痛苦. 用设计模式理念写出来的代码很晦涩.<< head first>&g ...
- 10.15 sed 命令实践
在n行前插入 [root@wen data]# sed '2i 106,dandan,CSO' person.txt101,oldboy,CEO106,dandan,CSO102,zhangyao,C ...
- http_load(基于linux平台的一种性能测试工具)
http_load 是运行在linux操作系统上的命令行测试工具, 用来对网站做压力测试.http_load以并行复用的方式运行,用以测试web服务器的吞吐量和负载.但是它不同于大多数压力测试工具, ...
- js中的$符
js中的$代表什么意思呢? 首先js的作用是什么呢?是修饰网页动态内容的.那么修饰就需要定位主题,比如你把html比喻一个美女,让她唱一首歌.那么首先你要定位出是你想让哪个美女唱歌,通常我们用id来定 ...
- Oracle 11g 的卸载
Oracle 11g 的卸载 停止 oracle 相关的所有服务. 使用 Oracle 自带的 Universal Installer 卸载工具卸载 Oracle. 删除注册表 HKEY/LOCAL_ ...
- appium常见问题07_appium输入中文无效
前几天在appium android自动化测试过程中,使用send_keys()输入中文,发现只能输入字母和数字,输入中文无反应. 大家是否同样遇到过该问题,当大家同样遇到该问题时,在配置参数desi ...
- django-5-使用数据库
修改默认数据库 django默认数据库为 SQLite3,若需要修改,比如改成mysql,则需要修改与settings.py文件同路径的__init__.py文件,添加如下内容: import pym ...