这篇文章分享一下关于早期版本Spring集成SpringMVC时的一个重要概念:父子容器。
关于父子容器,主要有几个点需要了解:
(1)何为父子容器,怎么产生的,父子容器之间的联系?
(2)父子容器各自Bean在包扫描初始化时怎么被自动分配到对应容器里?
(3)在父子容器中获取Bean时有什么先后顺序,怎么验证这个结论?
(4)新版本的Spring+SpringMVC对于父子容器是怎么处理的?
首先了解何为父子容器,这个概念是什么原因导致的?
父子容器是早期版本3.0前Spring与SpringMVC整合时产生的一个概念,父子容器顾名思义就是一个WEB应用中包含多个Spring的容器,为了让这些容器彼此之间进行关联就采用父子节点的方式进行关联,这就是父子容器。在早期版本Spring和SpringMVC都有各自的applicationContext/spring配置文件,这两个配置文件会产生各自的context容器实例。那么Spring和SpringMVC的容器在设计时就设计成了一种父子关系,Spring容器为父容器,SpringMVC为子容器,子容器可以引用父容器中的Bean,而父容器不可以引用子容器中的Bean。但是在现在的版本这个父子容器已经被默认关闭了,也就是说后面版本默认只有一个容器了,后面通过SpringMVC源码以及测试代码进行验证。
关于父子容器介绍以及结构图介绍,官网中1.1.1. Context Hierarchy章节有简短介绍:
上图中显示了2个WebApplicationContext实例,为了进行区分,分别称之为:Servlet WebApplicationContext、Root WebApplicationContext。 其中:
- Servlet WebApplicationContext:这是对web层进行配置,如控制器(controller)、视图解析器(view resolvers)等相关的bean。通过spring mvc中提供的DispatchServlet来加载配置,通常情况下,配置文件的名称为spring.xml。
- Root WebApplicationContext:这是对J2EE三层架构中的service层、dao层进行配置,如业务bean,数据源(DataSource)等。通常情况下,配置文件的名称为applicationContext.xml。在web应用中,其一般通过ContextLoaderListener来加载。
父子容器都存在于DispatcheServlet当中,这两个配置文件在web.xml中有所体现,其中spring.xml是作为参数标签放在DispatcherServlet中,applicationContext.xml则作为全局配置由ContextLoaderListener来加载。
<!-- Spring MVC 配置 并添加监听-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- ContextLoaderListener监听器配置:web应用启动时能加载Spring环境,
由于是web应用没有main方法,因此使用这个监听器可被Tomcat启动时加载,作为程序入口 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- DispatcherServlet转发器配置,会加载classpath:spring.xml -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name