热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ContextLoaderListener在Spring中的角色/用途?-Role/PurposeofContextLoaderListenerinSpring?

IamlearningSpringFrameworkwhichisbeingusedinmyproject.IfoundtheContextLoaderListener

I am learning Spring Framework which is being used in my project. I found the ContextLoaderListener entry in my web.xml file. But could not figure out how exactly it helps a developer?

我正在学习Spring框架,它正在我的项目中使用。我在web中找到了ContextLoaderListener条目。xml文件。但却搞不懂它到底是如何帮助开发人员的呢?

In the official documentation of ContextLoaderListener it says it is to start WebApplicationContext. Regarding WebApplicationContext JavaDocs say:

在ContextLoaderListener的官方文档中,它说要启动WebApplicationContext。关于WebApplicationContext JavaDocs说:

Interface to provide configuration for a web application.

为web应用程序提供配置的接口。


But I am not able to understand what I am achieving with ContextLoaderListener which internally initializes the WebApplicationContext ?

但是我不能理解我用ContextLoaderListener实现了什么,它在内部初始化WebApplicationContext ?

As per my understanding, ContextLoaderListener reads the Spring configuration file (with value given against contextConfigLocation in web.xml), parses it and loads the singleton bean defined in that config file. Similarly when we want to load prototype bean, we will use same webapplication context to load it. So we initialize the webapplication with ContextLoaderListener so that we read/parse/validate the config file in advance and whenever we wan to inject dependency we can straightaway do it without any delay. Is this understanding correct?

根据我的理解,ContextLoaderListener读取Spring配置文件(根据web.xml中的contextconfig提供的值),解析并加载在配置文件中定义的单例bean。类似地,当我们想加载原型bean时,我们将使用相同的webapplication上下文来加载它。所以我们用ContextLoaderListener来初始化webapplication,这样我们就可以提前读取/解析/验证配置文件,并且当我们想要注入依赖项时,我们可以毫不延迟地进行。这是正确的理解吗?

14 个解决方案

#1


89  

Your understanding is correct. The ApplicationContext is where your Spring beans live. The purpose of the ContextLoaderListener is two-fold:

你的理解是正确的。ApplicationContext是Spring bean所在的位置。ContextLoaderListener的目的是双重的:

  1. to tie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext and

    将应用程序上下文的生命周期与ServletContext的生命周期联系起来

  2. to automate the creation of the ApplicationContext, so you don't have to write explicit code to do create it - it's a convenience function.

    为了自动化应用程序上下文的创建,所以您不必编写显式的代码来创建它——这是一个方便的函数。

Another convenient thing about the ContextLoaderListener is that it creates a WebApplicationContext and WebApplicationContext provides access to the ServletContext via ServletContextAware beans and the getServletContext method.

ContextLoaderListener的另一个方便之处是它创建了一个WebApplicationContext, WebApplicationContext通过ServletContextAware bean和getServletContext方法提供对ServletContext的访问。

#2


39  

ContextLoaderListener is optional. Just to make a point here: you can boot up a Spring application without ever configuring ContextLoaderListener, just a basic minimum web.xml with DispatcherServlet.

ContextLoaderListener是可选的。这里要说明一点:您可以启动一个Spring应用程序,而不需要配置ContextLoaderListener,这只是一个基本的最小web。DispatcherServlet xml。

Here is what it would look like:

它是这样的:

web.xml

web . xml



  Some Minimal Webapp
     
    index.jsp    
  

  
    dispatcher
    
      org.springframework.web.servlet.DispatcherServlet
    
    1
  

  
    dispatcher
    *.do
  

Create a file called dispatcher-servlet.xml and store it under WEB-INF. Since we mentioned index.jsp in welcome list, add this file under WEB-INF.

创建一个名为dispatcher-servlet的文件。将它存储在WEB-INF中。因为我们提到的指数。在欢迎列表中的jsp,在WEB-INF下添加此文件。

dispatcher-servlet.xml

dispatcher-servlet.xml

In the dispatcher-servlet.xml define your beans:

dispatcher-servlet。xml bean定义:




    
      ...
    
    
      ...
             

    
    
    
    

    
    
      
      
      
    

#3


20  

For a simple Spring application, you don't have to define ContextLoaderListener in your web.xml; you can just put all your Spring configuration files in :

对于一个简单的Spring应用程序,您不必在web.xml中定义ContextLoaderListener;您可以将所有Spring配置文件放在 :


    hello
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:spring/mvc-core-config.xml, classpath:spring/business-config.xml
    
    1

For a more complex Spring application, where you have multiple DispatcherServlet defined, you can have the common Spring configuration files that are shared by all the DispatcherServlet defined in the ContextLoaderListener:

对于更复杂的Spring应用程序,其中定义了多个DispatcherServlet,您可以拥有由ContextLoaderListener中定义的所有DispatcherServlet共享的公共Spring配置文件:


    contextConfigLocation
    classpath:spring/common-config.xml


    org.springframework.web.context.ContextLoaderListener



    mvc1
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:spring/mvc1-config.xml
    
    1



    mvc2
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:spring/mvc2-config.xmll
    
    1

Just keep in mind, ContextLoaderListener performs the actual initialization work for the root application context.

请记住,ContextLoaderListener将为根应用程序上下文执行实际的初始化工作。

I found this article helps a lot: Spring MVC – Application Context vs Web Application Context

我发现这篇文章有很多帮助:Spring MVC—应用程序上下文vs Web应用程序上下文

#4


9  

The blog, "Purpose of ContextLoaderListener – Spring MVC" gives a very good explanation.

博客“ContextLoaderListener - Spring MVC的用途”给出了一个很好的解释。

According to it, Application-Contexts are hierarchial and hence DispatcherSerlvet's context becomes the child of ContextLoaderListener's context. Due to which, technology being used in the controller layer (Struts or Spring MVC) can independent of root context created ContextLoaderListener.

根据它,应用程序上下文是层次的,因此DispatcherSerlvet的上下文成为ContextLoaderListener上下文的子上下文。因此,在控制器层(Struts或Spring MVC)中使用的技术可以独立于创建ContextLoaderListener的根上下文。

#5


2  

When you want to put your Servlet file in your custom location or with custom name, rather than the default naming convention [servletname]-servlet.xml and path under Web-INF/ ,then you can use ContextLoaderListener.

当您希望将Servlet文件放在自定义位置或使用自定义名称时,而不是使用默认的命名约定[servletname]-servlet。在Web-INF/下的xml和路径,然后可以使用ContextLoaderListener。

#6


2  

Basically you can isolate your root application context and web application context using ContextLoaderListner.

基本上,您可以使用ContextLoaderListner隔离根应用程序上下文和web应用程序上下文。

The config file mapped with context param will behave as root application context configuration. And config file mapped with dispatcher servlet will behave like web application context.

使用上下文param映射的配置文件将表现为根应用程序上下文配置。与dispatcher servlet映射的配置文件将表现为web应用程序上下文。

In any web application we may have multiple dispatcher servlets, so multiple web application contexts.

在任何web应用程序中,我们可能有多个dispatcher servlet,因此有多个web应用程序上下文。

But in any web application we may have only one root application context that is shared with all web application contexts.

但是在任何web应用程序中,我们可能只有一个根应用程序上下文与所有web应用程序上下文共享。

We should define our common services, entities, aspects etc in root application context. And controllers, interceptors etc are in relevant web application context.

我们应该在根应用程序上下文中定义我们的公共服务、实体、方面等。控制器、拦截器等都在相关的web应用程序上下文中。

A sample web.xml is

一个示例web。xml是




    
        org.springframework.web.context.ContextLoaderListener
    
    
        contextClass
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    
    
        contextConfigLocation
        example.config.AppConfig
    
    
        restEntryPoint
        org.springframework.web.servlet.DispatcherServlet
        
            contextClass
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        
        
            contextConfigLocation
            example.config.RestConfig
               
        1
    
    
        restEntryPoint
        /rest/*
    
    
        webEntryPoint
        org.springframework.web.servlet.DispatcherServlet
        
            contextClass
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        
        
            contextConfigLocation
            example.config.WebConfig
               
        1
      
    
        webEntryPoint
        /
    

 

Here config class example.config.AppConfig can be used to configure services, entities, aspects etc in root application context that will be shared with all other web application contexts (for example here we have two web application context config classes RestConfig and WebConfig)

这里example.config配置类。AppConfig可用于配置根应用程序上下文中的服务、实体、方面等,这些内容将与所有其他web应用程序上下文共享(例如,我们有两个web应用程序上下文配置类RestConfig和WebConfig)

PS: Here ContextLoaderListener is completely optional. If we will not mention ContextLoaderListener in web.xml here, AppConfig will not work. In that case we need to configure all our services and entities in WebConfig and Rest Config.

这里的ContextLoaderListener是完全可选的。如果我们不在web中提到ContextLoaderListener的话。在这里,AppConfig将不起作用。在这种情况下,我们需要在WebConfig和Rest Config中配置所有的服务和实体。

#7


1  

ContextLoaderListner is a Servlet listener that loads all the different configuration files (service layer configuration, persistence layer configuration etc) into single spring application context.

ContextLoaderListner是一个Servlet侦听器,它将所有不同的配置文件(服务层配置、持久性层配置等)加载到单个spring应用程序上下文。

This helps to split spring configurations across multiple XML files.

这有助于在多个XML文件中分割spring配置。

Once the context files are loaded, Spring creates a WebApplicationContext object based on the bean definition and stores it in the ServletContext of your web application.

加载上下文文件之后,Spring根据bean定义创建一个WebApplicationContext对象,并将其存储在web应用程序的ServletContext中。

#8


0  

It will give you point of hook to put some code that you wish to be executed on web application deploy time

它将使您能够在web应用程序部署时放置一些希望执行的代码

#9


0  

Your understanding is correct. I wonder why you don't see any advantages in ContextLoaderListener. For example, you need to build a session factory (to manage database). This operation can take some time, so it's better to do it on startup. Of course you can do it with init servlets or something else, but the advantage of Spring's approach is that you make configuration without writing code.

你的理解是正确的。我想知道为什么在ContextLoaderListener中看不到任何优势。例如,您需要构建一个会话工厂(用于管理数据库)。这个操作需要一些时间,所以最好在启动时进行。当然,您可以使用init servlet或其他方法来完成它,但是Spring的方法的优点是无需编写代码就可以进行配置。

#10


0  

If we write web.xml without ContextLoaderListener then we cant give the athuntication using customAuthenticationProvider in spring security. Because DispatcherServelet is the child context of ContextLoaderListener, customAuthenticationProvider is the part of parentContext that is ContextLoaderListener. So parent Context cannot have the dependencies of child context. And so it is best practice to write spring-context.xml in contextparam instead of write it in the initparam.

如果我们写网页。如果没有ContextLoaderListener的xml,那么我们就不能在spring security中使用customAuthenticationProvider进行athuntication。因为DispatcherServelet是ContextLoaderListener的子上下文,customAuthenticationProvider是parentContext的一部分,它是ContextLoaderListener。因此父上下文不能具有子上下文的依赖关系。因此,最好的做法是编写spring上下文。xml在contextparam中而不是在initparam中编写。

#11


0  

I believe its real use comes when you want to have more than one config files or you have xyz.xml file instead of applicationcontext.xml for eg

我相信它的真正用途是当你想要一个以上的配置文件或者你有xyz。xml文件而不是应用程序上下文。xml如

contextConfigLocation /WEB-INF/training-service.xml, /WEB-INF/training-data.xml

contextConfigLocation <参数值> / web - inf /培训服务。xml / web - inf /训练数据。xml

Another approach to ContextLoaderListener is using ContextLoaderServlet like below

ContextLoaderListener的另一种方法是使用ContextLoaderServlet,如下所示

context org.springframework.web.context.ContextLoaderServlet 1

org.springframework.web.context上下文。ContextLoaderServlet 1

#12


0  

Listener class - Listens on an event (Eg.. Server startup/shutdown)

监听器类——监听事件(如……)服务器启动/关闭)

ContextLoaderListener -

ContextLoaderListener -

  1. Listens during server start up/shutdown
  2. 在服务器启动/关闭期间监听
  3. Takes the Spring configuration files as input and creates the beans as per configuration and make it ready (destroys the bean during shutdown)
  4. 将Spring配置文件作为输入,并按配置创建bean并使其准备就绪(在关闭期间销毁bean)
  5. Configuration files can be provided like this in web.xml

    在web.xml中可以像这样提供配置文件

    contextConfigLocation  
    /WEB-INF/dispatcher-servlet.xml  
    

#13


0  

In the context of spring framework purpose of ContextLoaderListener is to load the other beans in your application such as the middle-tier and data-tier components that drive the back end of the application.

在spring框架上下文中,ContextLoaderListener的目的是加载应用程序中的其他bean,比如驱动应用程序后端的中间层和数据层组件。

#14


0  

enter image description hereThis Bootstrap listener is to start up and shut down Spring's root WebApplicationContext. As a web application can have multiple dispatcher servlet and each having its own application context containing controllers, view resolver, handler mappings etc But you might want to have service beans, DAO beans in root application context and want to use in all child application context(application context created by dispatcher servlets).

这个引导侦听器将启动并关闭Spring的根WebApplicationContext。作为一个web应用程序可以有多个dispatcher servlet,各有自己的应用程序上下文包含控制器,视图解析器处理程序映射等但是你可能想要服务bean,DAO类应用程序上下文根和想使用所有子应用程序上下文(dispatcher servlet创建的应用程序上下文)。

2nd use of this listener is when you want to use spring security.

这个监听器的第二个用途是当您想使用spring security时。


推荐阅读
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
author-avatar
我在等白色的雪
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有