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,这样我们就可以提前读取/解析/验证配置文件,并且当我们想要注入依赖项时,我们可以毫不延迟地进行。这是正确的理解吗?
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的目的是双重的:
to tie the lifecycle of the ApplicationContext
to the lifecycle of the ServletContext
and
将应用程序上下文的生命周期与ServletContext的生命周期联系起来
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的访问。
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定义:
...
...
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应用程序上下文
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的根上下文。
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。
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中配置所有的服务和实体。
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中。
0
It will give you point of hook to put some code that you wish to be executed on web application deploy time
它将使您能够在web应用程序部署时放置一些希望执行的代码
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的方法的优点是无需编写代码就可以进行配置。
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中编写。
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如
Another approach to ContextLoaderListener is using ContextLoaderServlet like below
ContextLoaderListener的另一种方法是使用ContextLoaderServlet,如下所示
0
Listener class - Listens on an event (Eg.. Server startup/shutdown)
监听器类——监听事件(如……)服务器启动/关闭)
ContextLoaderListener -
ContextLoaderListener -
Configuration files can be provided like this in web.xml
在web.xml中可以像这样提供配置文件
contextConfigLocation
/WEB-INF/dispatcher-servlet.xml
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,比如驱动应用程序后端的中间层和数据层组件。
0
This 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时。