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的类






































原创粉丝点击