`
反求诸己
  • 浏览: 535370 次
  • 性别: Icon_minigender_1
  • 来自: 湖南娄底
社区版块
存档分类
最新评论

spring 的三种启用模式

 
阅读更多

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < web-app   xmlns = "http://java.sun.com/xml/ns/j2ee"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   version = "2.4"   
  4.     xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >   
  5.     <!-- spring  有三种启用模式 1ContextLoaderServlet 2.ContextLoaderListener 3.ContextLoaderPlugIn-->   
  6.     < context-param >   
  7.         < param-name > contextConfigLocation </ param-name >   
  8.         < param-value > /WEB-INF/applicationContext*.xml </ param-value >   
  9.     </ context-param >   
  10.     < servlet >   
  11.         < servlet-name > action </ servlet-name >   
  12.         < servlet-class >   
  13.             org.apache.struts.action.ActionServlet  
  14.         </ servlet-class >   
  15.         < init-param >   
  16.             < param-name > config </ param-name >   
  17.             < param-value >   
  18.                 /WEB-INF/struts-config.xml,/WEB-INF/struts-config-framework.xml  
  19.             </ param-value >   
  20.         </ init-param >   
  21.         < init-param >   
  22.             < param-name > debug </ param-name >   
  23.             < param-value > 3 </ param-value >   
  24.         </ init-param >   
  25.         < init-param >   
  26.             < param-name > detail </ param-name >   
  27.             < param-value > 3 </ param-value >   
  28.         </ init-param >   
  29.         < load-on-startup > 0 </ load-on-startup >   
  30.     </ servlet >   
  31.     <!-- Action -->   
  32.     < servlet-mapping >   
  33.         < servlet-name > action </ servlet-name >   
  34.         < url-pattern > *.do </ url-pattern >   
  35.     </ servlet-mapping >   
  36.     <!-- 欢迎界面 -->   
  37.     < welcome-file-list >   
  38.         < welcome-file > index.html </ welcome-file >   
  39.     </ welcome-file-list >   
  40.     <!-- Spring过滤中文字符集 -->   
  41.     < filter >   
  42.         < filter-name > SetCharacterEncoding </ filter-name >   
  43.         < filter-class >   
  44.             org.springframework.web.filter.CharacterEncodingFilter  
  45.         </ filter-class >   
  46.         < init-param >   
  47.             < param-name > encoding </ param-name >   
  48.             < param-value > UTF-8 </ param-value >   
  49.         </ init-param >   
  50.     </ filter >   
  51.     <!-- 要过滤得类型 -->   
  52.     < filter-mapping >   
  53.         < filter-name > SetCharacterEncoding </ filter-name >   
  54.         < url-pattern > *.jsp </ url-pattern >   
  55.     </ filter-mapping >   
  56.     < filter-mapping >   
  57.         < filter-name > SetCharacterEncoding </ filter-name >   
  58.         < url-pattern > *.do </ url-pattern >   
  59.     </ filter-mapping >   
  60.     <!-- 注册Spring的request作用域 -->   
  61.     < listener >   
  62.         < listener-class >   
  63.             org.springframework.web.context.request.RequestContextListener  
  64.         </ listener-class >   
  65.     </ listener >   
  66.     <!--  
  67.         request  
  68.         request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例:  
  69.         request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:  
  70.         如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:  
  71.         < web-app >   
  72.         org.springframework.web.context.request.RequestContextListener  
  73.         ...  
  74.         < listener >   
  75.         < listener-class > org.springframework.web.context.request.RequestContextListener </ listener-class >   
  76.         </ listener >   
  77.         ...  
  78.         </ web-app >   
  79.           
  80.     -->   
  81.     <!-- OpenSessionInView -->   
  82.     < filter >   
  83.         < filter-name > OpenSessionInViewFilter </ filter-name >   
  84.         < filter-class >   
  85.             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
  86.         </ filter-class >   
  87.         < init-param >   
  88.             < param-name > singleSession </ param-name >   
  89.             < param-value > true </ param-value >   
  90.         </ init-param >   
  91.     </ filter >   
  92.     <!--  
  93.         org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  
  94.         延迟加载的实现的原理是cglib动态字节码  
  95.         Hibernate对延迟加载的实现原理是CGLIB动态字节码生成技术,即返回的实体并非真正的实体对象,而是经过CGLIB处理后的代理实体,当调用某一未经加载的属性时,代理实体就可以截获这一调用,然后由Hibernate实现动态加载。  
  96.           
  97.         如果要使用Hibernate的延迟加载特性,则渲染视图阶段不能关闭事务,因此,事务的范围变为整个HTTP请求的周期。  
  98.           
  99.         采用OpenSessionInView模式可以将事务范围界定在请求开始和渲染视图结束后,使得Hibernate的Session在视图渲染时仍有效。有两种方式实现OpenSessionInView模式,一种是使用Spring提供的OpenSessionInViewInterceptor,如果采用Spring MVC框架,可以将这个Interceptor加入到Controller的拦截器链中,事务在Controller处理前开始,在视图渲染后结束,如图11-17所示。  
  100.           
  101.           
  102.           
  103.         如果Web层没有采用Spring的MVC框架,而是使用Struts等其他MVC框架,甚至没有使用MVC框架,此时,就无法定义Interceptor,只能采用Filter来实现OpenSessionInView模式。  
  104.           
  105.         OpenSessionInViewFilter是Spring提供的一个Filter。在OpenSessionInViewFilter模式下,所有的HTTP请求都将被OpenSessionInViewFilter截获,事务在请求处理前开始,在请求处理完毕后结束,而不管采用何种MVC框架,甚至直接使用JSP,如图11-18所示。  
  106.           
  107.         org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(延迟加载,)  
  108.           
  109.         图11-18  
  110.           
  111.         两种方式各有优劣。OpenSessionInViewInterceptor只能用于Spring MVC,但是配置简单,无须过滤URL;OpenSessionInViewFilter适用范围更广,但是必须手动配置web.xml文件,并且必须正确过滤URL。  
  112.           
  113.         无论如何,采用以上两种方式的目的都是为了使用Hibernate的延迟加载特性。由于事务也是一种数据库资源,事务持续的时间越久,数据库资源被锁定也越久,应用程序的吞吐量就会降低。因此,要尽量将事务限定在最小的范围内  
  114.           
  115.     -->   
  116.     <!-- session的过滤控制用户在登录时的权限限制-->   
  117.     < filter >   
  118.         < filter-name > authorizen </ filter-name >   
  119.         < filter-class >   
  120.             org.springframework.web.filter.DelegatingFilterProxy  
  121.         </ filter-class >   
  122.         < init-param >   
  123.             < param-name > targetFilterLifecycle </ param-name >   
  124.             < param-value > true </ param-value >   
  125.         </ init-param >   
  126.     </ filter >   
  127.     <!-- 以前学习框架经常做登录页面的demo,输入正确的id+pwd就返回成功了。。可是这种模式无法阻止通过URL直接访问其他的页面,在一个非玩具系统中,控制未登录用户的页面访问权限是一个基本功能。  
  128.           
  129.         从实现思路讲,验证一个用户的有效登录,大多采用的是登入时候向Session写一个User认证信息,然后在访问每个页面前来判断Session中是否有认证信息。  
  130.           
  131.         if(session.get("User")==null)  
  132.         另外有很多网站提供记住登陆信息xx天,这种是结合了Cookie的认证信息存储。谈到这里,也可以仔细想想Cookie和Session的作用。比如卓越的购物车就是Cookie做的(因为关闭IE后再访问购物车还记得你的物品),而大多数群集Web服务器的信息也是采用Cookie(因为群集的Session同步开销很大),掌握了Cookie和Session的基本特性,这些都是理所当然的做法了。  
  133.           
  134.         一。下面说第一种拦截实现:基于javax.servlet.Filter  
  135.           
  136.         1.首先需要到web.xml注册一个filter  
  137.           
  138.         (这里是将authorityFilter这个类委托给spring来代理)  
  139.           
  140.     -->   
  141.     < filter-mapping >   
  142.         < filter-name > OpenSessionInViewFilter </ filter-name >   
  143.         < url-pattern > /* </ url-pattern >   
  144.     </ filter-mapping >   
  145.     < filter-mapping >   
  146.         < filter-name > authorizen </ filter-name >   
  147.         < url-pattern > *.do </ url-pattern >   
  148.     </ filter-mapping >   
  149.     <!-- 设置监听 -->   
  150.     < listener >   
  151.         < listener-class >   
  152.             org.springframework.web.context.ContextLoaderListener  
  153.         </ listener-class >   
  154.     </ listener >   
  155.     <!-- 今天有一个朋友问了我一个问题,他使用的是Hibernate/Spring/Struts架构,配置使用Spring的OpenSessionInView Filter,但是发现不生效,lazy的集合属性在页面访问的时候仍然报session已经关闭的错误。我和他一起检查了所有的配置和相关的代码,但是没有发现任何问题。经过调试发现,应用程序使用的Session和OpenSessionInView Filter打开的Session不是同一个,所以OpenSessionInView模式没有生效,但是为什么他们不使用同一个Session呢?  
  156.           
  157.         检查了一遍Spring的相关源代码,发现了问题的根源:  
  158.           
  159.         通常在Web应用中初始化Spring的配置,我们会在web.xml里面配置一个Listener,即:  
  160.           
  161.         Xml代码  -->   
  162.     < listener >   
  163.         < listener-class >   
  164.             org.springframework.web.context.ContextLoaderListener  
  165.         </ listener-class >   
  166.     </ listener >   
  167.     <!-- 启动spring的一种模式,运行之后要去找上面的<context-param></context-param> -->   
  168.     < listener >   
  169.         < listener-class >   
  170.             org.springframework.web.context.ContextLoaderListener  
  171.         </ listener-class >   
  172.     </ listener >   
  173.   
  174.     <!-- 如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一个Spring的plugin:ContextLoaderPlugIn。  
  175.           
  176.         实际上ContextLoaderListener和ContextLoaderPlugIn的功能是重叠的,他们都是进行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener。  
  177.           
  178.         好了,但是你现在既需要Struts集成Spring,又需要OpenSessionInView模式,问题就来了!  
  179.           
  180.         由于ContextLoaderListener和ContextLoaderPlugIn功能重叠,都是初始化Spring,你不应该进行两次初始化,所以你不应该同时使用这两者,只能选择一个,因为你现在需要集成Struts,所以你只能使用ContextLoaderPlugIn。  
  181.           
  182.         但是令人困惑的是,ContextLoaderListener和ContextLoaderPlugIn有一个非常矛盾的地方!  
  183.           
  184.         ContextLoaderListener初始化spring配置,然后把它放在ServletContext对象里面保存:  
  185.           
  186.         [code:1]servletContext.setAttribute(  
  187.         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);[/code:1]  
  188.           
  189.         请注意,保存的对象的key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!  
  190.           
  191.         但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext对象里面保存:  
  192.           
  193.         [code:1]  
  194.         String attrName  =  getServletContextAttributeName ();  
  195.         getServletContext().setAttribute(attrName, wac);[/code:1]  
  196.           
  197.         这个attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一样的!  
  198.           
  199.         如果仅仅是名字不一样,问题还不大,你仍然可以放心使用ContextLoaderPlugIn,但是当你使用OpenSessionInView的时候,OpenSessionInViewFilter是使用哪个key取得spring配置的呢?  
  200.           
  201.         [code:1]WebApplicationContext wac  =  
  202.         WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext());[/code:1]  
  203.           
  204.         显然,OpenSessionInViewFilter是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个key去拿spring配置的!  
  205.           
  206.         我们整理一下思路:  
  207.           
  208.         ContextLoaderPlugIn保存spring配置的名字叫做attrName;  
  209.         ,ContextLoaderListener保存spring配置的名字叫做WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;  
  210.         而OpenSessionInView是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个名字去取得spring配置的!  
  211.         而你的应用程序却是按照attrName去取得spring的配置的!  
  212.           
  213.         所以,OpenSessionInView模式失效!  
  214.           
  215.         解决办法:  
  216.         修改ContextLoaderPlugIn代码,在getServletContext().setAttribute(attrName, wac);这个地方加上一行代码:  
  217.         getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);  
  218.           
  219.         或者修改OpenSessionInViewFilter,让它按照attrName去取得spring配置。  
  220.     -->   
  221.     <!-- 初始页面 -->   
  222.     < welcome-file-list >   
  223.         < welcome-file > /index.html </ welcome-file >   
  224.     </ welcome-file-list >   
  225.   
  226. </ web-app >  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics