spring和spring mvc 中有关父子容器自我归纳
来源:互联网 发布:ff14人族捏脸数据分享 编辑:程序博客网 时间:2024/06/06 02:46
这几天看到了有关spring 容器的一些知识点,所以自己也就稍微总结一下自己的理解。
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。至于ApplicationContext.xml这个配置文件部署在哪,如何配置多个xml文件,查看它的API文档,在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
首先从web.xml文件中说起
首先在加载web项目的时候,spring 的ContextLoaderListener(监听器)会监听到然后就加载spring容器(相当于父容器)
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener 类继承了ContextLoader类并且实现了ServletContextListener接口
1.ContextLoaderListener 调用contextInitialized(ServletContextEvent event)初始化web application
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
//初始化ContextLoader 对象
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
//ContextLoader 调用 初始化initWebApplicationContext 方法
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
对应的ContextLoaderListener 也有一个销毁web application容器的方法
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
2.ContextLoader 类中的initWebApplicationContext(event.getServletContext()) 方法进行初始化工作
方法里面代码太多 就不一一列举,因为也没有完全看明白
二:加载需要经过DispatcherServlet 控制的Controller类,相当于spring 中的子容器,
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml)
DispatcherServlet to 》处理映射器(handler-mapping) to 》适配器(HandlerAdapter) to 》执行handler(就是我们平时说的controller类 但是要实现controller 接口)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
由于spring 容器和springmvc容器相当于是一个父子关系,如下图
因为在Spring mvc 项目中会存在两个容器(父容器与子容器)所以在写配置文件时也需要注意容器之间的访问规则
一般在springContext.xml文件中通过扫描器<context:component-scan base-package="cn.itcast.ssm"/> 扫描类
但是一般需要将Controller排除在外
<context:component-scan base-package="cn.itcast.ssm" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="cn.itcast.ssm" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
因为spring-mvc.xml中需要扫描的是Controller类,所以在此文件中一定要将 Controller类扫描在其中,
<context:component-scan base-package="cn.itcast.ssm.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
Controller如果再srpingContext.xml容器中,那么在springmvc 中请求时就无法找到相应的Controller 因为 在子容器中没有注解为Controller的类
- spring和spring mvc 中有关父子容器自我归纳
- Spring MVC上下文父子容器
- spring和spring mvc父子容器--学习笔记
- Spring中父子容器的实现实例
- spring中父子容器的实现
- Spring中父子容器的实现实例
- Spring中父子容器的实现实例
- Spring 父子容器
- Spring中的父子容器
- spring 父子容器 事务管理
- Spring父子容器
- Spring的父子容器
- spring 父子容器
- Spring 父子容器概念
- Spring 三级父子容器
- spring 父子容器
- spring父子容器
- 理解spring父子容器
- 如何在GlobalMapper中打开谷歌卫星地图
- IDEA搭建Spark程序开发环境
- 负载均衡之轮询算法
- greenDao的基本使用及用法详解
- SAPUI5教程——ABAP环境下SAP Fiori 系统搭建以及开发实践
- spring和spring mvc 中有关父子容器自我归纳
- AWS EC2小结
- oracle学习使用--oracle安装都安装为企业版
- 请问js对象属性值为什么用数组也可以访问
- 算法细节系列(34):再见字符串(2)
- 151组最易混淆的单词
- 二维数组中的查找(java版)
- Mysql有两种存储引擎:InnoDB与Myisam
- Java中序列化的serialVersionUID作用