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.

web.xml
<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.

Starting Spring 3.x, method 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.

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:

org/springframework/web/context/ContextLoader.java
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
 
//Where attibute is defined in /org/springframework/web/context/WebApplicationContext.java as
 
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";

To get root application context in Spring controller, you can use WebApplicationContextUtils class.

Controller.java
@Autowired
ServletContext 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

  1. ContextLoaderListener creates root application context.
  2. DispatcherServlet entries create one child application context per servlet entry.
  3. Child contexts can access beans defined in root context.
  4. Beans in root context cannot access beans in child contexts (directly).
  5. All contexts are added to ServletContext.
  6. You can access root context using WebApplicationContextUtils class.

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的更多相关文章

  1. 【转】ContextLoaderListener和DispatcherServlet加载内容的区别

    一.ContextLoaderListener加载内容 二.DispatcherServlet加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启 ...

  2. web.xml中的ContextLoaderListener和DispatcherServlet区别

    ContextLoaderListener和DispatcherServlet都会在Web容器启动的时候加载一下bean配置. 区别在于: DispatcherServlet一般会加载MVC相关的be ...

  3. 使用shiro 框架 报错No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?

    1.问题描述:ssm 框架中使用shiro  中出现问题 原来web.xml 文件如下: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, ...

  4. 【转】ContextLoaderListener 和 DispatcherServlet

    转载地址: http://www.guoweiwei.com/archives/797 DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供S ...

  5. ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

    spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件 ...

  6. Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

    http://www.lai18.com/content/9755931.html Spring 容器(Spring 的上下文) https://my.oschina.net/jast90/blog/ ...

  7. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别

    一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...

  8. 【Spring】浅谈ContextLoaderListener及其上下文与DispatcherServlet的区别

    一般在使用SpingMVC开发的项目中,一般都会在web.xml文件中配置ContextLoaderListener监听器,如下: <listener> <listener-clas ...

  9. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】

    原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...

随机推荐

  1. Arrays.asList()报错java.lang.UnsupportedOperationException

    问题: 使用工具类Arrays.asList()方法把数组转换成集合时,不能使用修改集合相关的方法,比如add,remove.这个ArrayList是Arrays类自己定义的一个内部类!这个内部类没有 ...

  2. 广播接收者实现IP拨号

    广播接收者实现IP拨号 效果图: 实现的功能就是自动监听我们要拨打的号码,在我们拨打的号码前加上179521 分析: 1.敲个类来继承广播接收者 并且将从打电话应用位置获取的号码加上179521,并将 ...

  3. 转载:AWR介绍使用

    转载自 http://www.cnblogs.com/lanzi/archive/2011/03/07/1975096.html 自动工作负载库(Automatic Workload Reposito ...

  4. bzoj 1233: [Usaco2009Open]干草堆tower 【想法题】

    首先这题的$n^3$的DP是比较好想的 $f[i][j]$表示用前$i$包干草 且最顶层为第$j+1$包到第$i$包 所能达到的最大高度 然而数据范围还是太大了 因此我们需要去想一想有没有什么单调性 ...

  5. python接口自动化测试三十五:用BeautifulReport生成报告

    GitHub传送门:https://github.com/TesterlifeRaymond/BeautifulReport 配置BeautifulReport 下载.解压并修改名字为Beautifu ...

  6. java构造器内部多态方法

    public class TestC { public static void main(String []args) { new Graph(5); }}class Grp{ void draw() ...

  7. Python 多进程异常处理

    前言 最近项目用到了Python作为网站的前端,使用的框架是基于线程池的Cherrypy,但是前端依然有一些比较‘重’的模块.由于python的多线程无法很好的利用多核的性质,所以觉得把这些比较‘重’ ...

  8. 算法中时间复杂度概括——o(1)、o(n)、o(logn)、o(nlogn)

    在描述算法复杂度时,经常用到 o(1), o(n), o(logn), o(nlogn) 来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义:这是算法的时空复杂度的表示.不仅仅用于表示时间复 ...

  9. (DP)HDU - 1003 Max Sum

    这是一道DP入门题目,知识点是“最大连续子序列” 题目大意:给你一个长度为n的数字序列,取其中一段连续的序列,要求和最大: 分析:这是一道裸题,没有什么花里胡哨的东西,主要是写出状态转移方程 dp[i ...

  10. 4.jmeter在线并发的怎样设置

    4.1Jmeter 快速入门教程(一) - 认识jmeter和google插件 4.2Jmeter 快速入门教程(二)--创建简单web测试 打印 E-mail 4.3Jmeter 快速入门教程(三- ...