关于spring框架中的多个容器 [英] About multiple containers in spring framework

查看:91
本文介绍了关于spring框架中的多个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在典型的 Spring MVC 项目中,有两个容器":一个由 ContextLoaderListener 创建,另一个由 DispatchServlet 创建.

In a typical Spring MVC project there two "containers": One created by ContextLoaderListener and the other created by DispatchServlet.

我想知道,这真的是两个IoC容器实例吗?(我看到两个bean配置文件,一个是root-context.xml另一个是servlet-context.xml)

I want to know, are these really two IoC container instance?( I see two bean config files, one is root-context.xml the other is servlet-context.xml)

如果有2个容器,那是什么关系?

If there are 2 containers, then what's the relationship?

在一个容器中声明的 bean 可以在另一个容器中使用吗?

Can the beans declared in one container be used in the other?

推荐答案

来自 Spring 官网:

接口org.springframework.context.ApplicationContext代表 Spring IoC 容器并负责实例化、配置和组装上述 bean.容器获得关于要实例化哪些对象的指令,通过读取配置元数据来配置和组装.这配置元数据以 XML、Java 注释或Java代码.

The interface org.springframework.context.ApplicationContext represents 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.

再次来自官方文档:

在 Web MVC 框架中,每个 DispatcherServlet 都有自己的WebApplicationContext,它继承了已经定义的所有bean根 WebApplicationContext.这些继承的bean可以是在特定于 servlet 的范围内被覆盖,您可以定义新的特定于给定 Servlet 实例的特定范围的 bean.

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.

现在来回答您的问题,正如here:

Now coming to your Question, as is stated here:

在 Spring Web Applications 中,有两种类型的容器,每一种它的配置和初始化方式不同.一个是应用程序上下文",另一个是Web 应用程序上下文".让我们先谈谈应用程序上下文".应用上下文是由 ContextLoaderListener 初始化的容器或web.xml中定义的ContextLoaderServlet和配置看起来像这样:

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>

在上面的配置中,我要求spring从匹配 *-context.xml 并创建应用程序的类路径从它的上下文.例如,此上下文可能包含组件例如中间层事务服务、数据访问对象,或您可能希望在整个过程中使用(和重用)的其他对象应用.每个应用程序将有一个应用程序上下文.

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.

另一个上下文是WebApplicationContext",它是孩子应用上下文的上下文.每个 DispatcherServlet 定义在Spring Web 应用程序将有一个关联的网络应用程序上下文.WebApplicationContext 的初始化是这样发生的:

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>

你以servlet的形式提供spring配置文件的名字初始化参数.这里重要的是要记住XML 的名称必须采用 -servlet 的形式.xml.在本例中,servlet 的名称是 platform-services因此,我们的 XML 的名称必须是 platform-service-servlet.xml.可以引用 ApplicationContext 中可用的任何 bean来自每个 WebApplicationContext.最好的做法是保持明确分离中间层服务,例如业务逻辑组件和数据访问类(通常在ApplicationContext) 和 web 相关的组件,例如控制器并查看解析器(在 WebApplicationContext 中定义)调度程序 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).

检查这些链接

applicationContext.xml 和 spring-servlet 的区别Spring 框架中的 .xml

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-basics

这篇关于关于spring框架中的多个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆