spring 关于spring框架中的多个容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18578143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
About multiple containers in spring framework
提问by lovespring
In a typical Spring MVC project there two "containers": One created by ContextLoaderListener and the other created by DispatchServlet.
在典型的 Spring MVC 项目中,有两个“容器”:一个由 ContextLoaderListener 创建,另一个由 DispatchServlet 创建。
I want to know, are these really two IoC container instance?( I see two bean config files, one is root-context.xmlthe other is servlet-context.xml)
我想知道,这真的是两个IoC容器实例吗?(我看到两个bean配置文件,一个是root-context.xml另一个servlet-context.xml)
If there are 2 containers, then what's the relationship?
如果有2个容器,那是什么关系?
Can the beans declared in one container be used in the other?
在一个容器中声明的 bean 可以在另一个容器中使用吗?
回答by beinghuman
From the Spring Official Website:
来自Spring 官网:
The interface
org.springframework.context.ApplicationContextrepresents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code.
该接口
org.springframework.context.ApplicationContext代表 Spring IoC 容器,负责实例化、配置和组装上述 bean。容器通过读取配置元数据来获取有关要实例化、配置和组装哪些对象的指令。配置元数据以 XML、Java 注释或 Java 代码表示。
Again from official Doc:
再次来自官方文档:
In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given Servlet instance.
在 Web MVC 框架中,每个 DispatcherServlet 都有自己的 WebApplicationContext,它继承了根 WebApplicationContext 中已经定义的所有 bean。这些继承的 bean 可以在 servlet 特定的范围内被覆盖,并且您可以定义新的特定于给定 Servlet 实例的范围特定的 bean。
Now coming to your Question, as is stated here:
现在来到你的问题,因为据称在这里:
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently. One is the “Application Context” and the other is the “Web Application Context”. Lets first talk about the “Application Context”. Application Context is the container initialized by a ContextLoaderListener or ContextLoaderServlet defined in the web.xml and the configuration would look something like this:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*-context.xml</param-value> </context-param>In the above configuration, I am asking spring to load all files from the classpath that match *-context.xml and create an Application Context from it. This context might, for instance, contain components such as middle-tier transactional services, data access objects, or other objects that you might want to use (and re-use) across the application. There will be one application context per application.
The other context is the “WebApplicationContext” which is the child context of the application context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. The initialization of the WebApplicationContext happens like this:
<servlet> <servlet-name>platform-services</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:platform-services-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>You provide the name of the spring configuration file as a servlet initialization parameter. What is important to remember here is that the name of the XML must be of the form -servlet. xml. In this example, the name of the servlet is platform-services therefore the name of our XML must be platform-service-servlet.xml. Whatever beans are available in the ApplicationContext can be referred to from each WebApplicationContext. It is a best practice to keep a clear separation between middle-tier services such as business logic components and data access classes (that are typically defined in the ApplicationContext) and web- related components such as controllers and view resolvers (that are defined in the WebApplicationContext per Dispatcher Servlet).
在 Spring Web Applications 中,有两种类型的容器,每种容器的配置和初始化方式都不同。一个是“应用程序上下文”,另一个是“Web 应用程序上下文”。让我们先谈谈“应用程序上下文”。应用程序上下文是由 web.xml 中定义的 ContextLoaderListener 或 ContextLoaderServlet 初始化的容器,配置如下所示:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:*-context.xml</param-value> </context-param>在上面的配置中,我要求 spring 从匹配 *-context.xml 的类路径中加载所有文件并从中创建一个应用程序上下文。例如,此上下文可能包含诸如中间层事务服务、数据访问对象或您可能希望在整个应用程序中使用(和重用)的其他对象等组件。每个应用程序将有一个应用程序上下文。
另一个上下文是“WebApplicationContext”,它是应用程序上下文的子上下文。Spring Web 应用程序中定义的每个 DispatcherServlet 都会有一个关联的 WebApplicationContext。WebApplicationContext 的初始化是这样发生的:
<servlet> <servlet-name>platform-services</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:platform-services-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>您提供 spring 配置文件的名称作为 servlet 初始化参数。这里要记住的重要一点是 XML 的名称必须采用 -servlet 的形式。xml。在这个例子中,servlet 的名称是 platform-services,因此我们的 XML 的名称必须是 platform-service-servlet.xml。可以从每个 WebApplicationContext 引用 ApplicationContext 中可用的任何 bean。最好的做法是在中间层服务(例如业务逻辑组件和数据访问类(通常在 ApplicationContext 中定义)和 Web 相关组件(例如控制器和视图解析器)(在每个 Dispatcher Servlet 的 WebApplicationContext)。
Check these links
检查这些链接
Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
Spring Framework中applicationContext.xml和spring-servlet.xml的区别
回答by Raunak Agarwal
There aren't two separate containers created. Typically, you want spring to instantiate the object declared in the servlet-context.xml when the object is required. So, you map the servlet-context.xml configuration file to the Dispatcher Servlet i.e. you want to initialize the object when a request hits the dispatcher servlet.
没有创建两个单独的容器。通常,当需要对象时,您希望 spring 实例化在 servlet-context.xml 中声明的对象。因此,您将 servlet-context.xml 配置文件映射到 Dispatcher Servlet,即您希望在请求到达调度程序 servlet 时初始化该对象。
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Where as, if you want to initialize the object and perform action when the context is being loaded you would declare the configuration file with in the context-paramtags of your deployment descriptor.
其中,如果您想在加载上下文时初始化对象并执行操作,您可以在context-param部署描述符的标签中声明配置文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
You could test this out by writing by declaring separate beans in the servlet-context.xml and root-context.xml and then, autowiring them in a custom Context Loader Listener class. You would find only the root-context instances are initialized and servlet-context beans are null.
您可以通过在 servlet-context.xml 和 root-context.xml 中声明单独的 bean,然后在自定义 Context Loader Listener 类中自动装配它们来测试这一点。您会发现只有根上下文实例被初始化,而 servlet 上下文 bean 为空。
回答by Premraj
ApplicationContexta registry of components (beans).ApplicationContextdefines the beans that are shared among all the servlets i.e. root context configuration for every web application.spring*-servlet.xmldefines the beans that are relatedWebApplicationContextshereDispatcherServlet.- Spring container can have either single or multiple
WebApplicationContexts.
ApplicationContext组件(bean)的注册表。ApplicationContext定义在所有 servlet 之间共享的 bean,即每个 Web 应用程序的根上下文配置。spring*-servlet.xml定义WebApplicationContexts此处相关的 beanDispatcherServlet。- Spring 容器可以有单个或多个
WebApplicationContexts.
回答by Sanchi Girotra
Spring MVC have atleast 2 container -
Spring MVC 至少有 2 个容器 -
Application Context declared by
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param>Servlet context declared by -
<servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
应用上下文声明
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param>由 - 声明的 Servlet 上下文
<servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
And a web application can define any number of DispatcherServlet's. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared. Thus can have any number of child containers.
并且 Web 应用程序可以定义任意数量DispatcherServlet的 。每个 servlet 都将在自己的命名空间中运行,使用映射、处理程序等加载自己的应用程序上下文。只有由 加载的根应用程序上下文(ContextLoaderListener如果有)才会被共享。因此可以有任意数量的子容器。


