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

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

问题描述

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



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



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



另一个容器可以使用?

解决方案

春季官方网站


界面 org.springframework.context.ApplicationContext
表示Spring IoC容器,负责
实例化,配置和组装上述豆类。
容器获取关于要实例化的对象的指令,
配置,并通过读取配置元数据进行组合。
配置元数据以XML,Java注释或
Java代码表示。


从官方文档:


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


现在来到你的问题,如这里


在Spring Web应用程序中,有两种类型的容器,每个
被配置和初始化不同。一个是
应用程序上下文,另一个是Web应用程序上下文。
首先谈谈应用程序上下文。应用程序上下文
是由ContextLoaderListener初始化的容器或
在web.xml中定义的ContextLoaderServlet,配置
将如下所示:

 <听者GT; 
< 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,它是应用程序上下文的子
上下文。每个DispatcherServlet在
中定义的一个Spring Web应用程序将有一个关联的
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。
ApplicationContext中可用的任何bean可以从每个WebApplicationContext引用
。在中间层服务(例如业务逻辑
组件和数据访问类(通常在
ApplicationContext中定义))和Web相关组件之间保持
的清除是最佳做法例如控制器
和视图解析器(在WebApplicationContext每
Dispatcher Servlet中定义)。


检查这些链接



Spring Framework中的applicationContext.xml和spring-servlet.xml之间的区别



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


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

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)

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

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

解决方案

From the Spring Official Website:

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.

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.

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).

Check these links

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

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

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

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