码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 中关于父上下文与子上下文的关系

时间:2015-07-26 15:33:17      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

最近在用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);

 

Spring 中关于父上下文与子上下文的关系

标签:

原文地址:http://www.cnblogs.com/jinfang134/p/4677756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!