zoukankan      html  css  js  c++  java
  • Spring 中关于父上下文与子上下文的关系

    最近在用Ehcache做spring的缓存,但是照着别人的博文来做,缓存就是不起作用,我是采用分开配置的方式。

    配置文件如下:

     web.xml文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     id="WebApp_ID" 
     version="3.1"
     >
        
      <display-name>SpringMVCLesson</display-name> 
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            classpath:spring.xml,
            classpath:spring-ehcache.xml,
            classpath:spring-freemark.xml
            </param-value>
        </context-param>     
       <listener>
            <description>spring监听器</description>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
       
        
        <!-- 解决办法可采用spring自带的过滤技术,对所有页面间参数的传递设置统一的字符编码。-->
        <filter>  
          <filter-name>characterEncodingFilter</filter-name>  
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
          <init-param>  
           <param-name>encoding</param-name>  
           <param-value>UTF-8</param-value>  
          </init-param>  
          <init-param>  
           <param-name>forceEncoding</param-name>  
           <param-value>true</param-value>  
          </init-param>  
        </filter>  
        <filter-mapping>  
          <filter-name>characterEncodingFilter</filter-name>  
          <url-pattern>/*</url-pattern>  
        </filter-mapping>  
    
         <servlet>
            <servlet-name>SpringMVCLesson</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springservlet-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->
        </servlet>
         <!-- Spring MVC配置文件结束 -->    
        <servlet-mapping>
            <servlet-name>SpringMVCLesson</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
    </web-app>

    后来经过多次尝试才发现了问题的所在,因为父上下文与子上下文的关系,父上下文不能访问子上下文中的内容,如果在子上下文springservlet-config.xml中也用<context:component-scan base-package="com.gdnyt.controller,com.gdnyt.services" />,那么定义在父上文中的扫描spring.xml文件中的

    <context:component-scan 
        base-package="com.gdnyt.services,
        com.gdnyt.dao,
        com.gdnyt.dao.interfaces,com.gdnyt.service.interfaces,com.gdnyt.model
        com.gdnyt.model,
        com.gdnyt.myview
        " />

    将不可用,导致注入失败,从而缓存无法正常运行。
        

    以下援引自博文http://bbs.csdn.net/topics/390551972中leighton11的回复

    Spring会创建一个WebApplicationContext上下文,称为父上下文(父容器) ,保存在 ServletContext中,key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值。
    可以使用Spring提供的工具类取出上下文对象:WebApplicationContextUtils.getWebApplicationContext(ServletContext);
     
    DispatcherServlet是一个Servlet,可以同时配置多个,每个 DispatcherServlet有一个自己的上下文对象(WebApplicationContext),称为子上下文(子容器),子上下文可以访问父上下文中的内容,但父上下文不能访问子上下文中的内容。 它也保存在 ServletContext中,key是"org.springframework.web.servlet.FrameworkServlet.CONTEXT"+Servlet名称。当一个Request对象产生时,会把这个子上下文对象(WebApplicationContext)保存在Request对象中,key是DispatcherServlet.class.getName() + ".CONTEXT"。
    可以使用工具类取出上下文对象:RequestContextUtils.getWebApplicationContext(request);

  • 相关阅读:
    Ubuntu配置sublime text 3的c编译环境
    ORA-01078错误举例:SID的大写和小写错误
    linux下多进程的文件拷贝与进程相关的一些基础知识
    ASM(四) 利用Method 组件动态注入方法逻辑
    基于Redis的三种分布式爬虫策略
    Go语言并发编程总结
    POJ2406 Power Strings 【KMP】
    nyoj 会场安排问题
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor.
    Java的String、StringBuffer和StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/jinfang134/p/4677756.html
Copyright © 2011-2022 走看看