Spring管理filter和servlet

在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("beanName")来获得对象的引用,这实际上是使用了依赖查找来获得对象,并且在filter或者servlet代码中硬编码了应用对象的bean名字。为了能在filter或者servlet中感知spring中bean,可采用如下步骤来实现:


1-  将filter或者servlet作为bean定义在context.xml文件中,和要应用的bean定义放在一起;
2-  实现一个filter代理或者servlet代理,该代理用WebApplicationContext来获得在context.xml中定义的filter或者servlet的对象,并将任务委托给context.xml中定义的filter或者servlet
3-  在web.xml中用ContextLoaderListener来初始化spring  的context,同时在filter代理或者servlet代理的定义中用初始化参数来定义context.xml中filter或者servlet的bean名字(或者直接受用代理的名称获得相应的filter或者servlet的名称)。
4-  在web.xml中定义filter代理或者servlet代理的mapping.

利用这种方式就将filter或者servlet和业务对象的依赖关系用spring 来进行管理,并且不用在servlet中硬编码要引用的对象名字。

配置DelegatingServletProxy的常用方法见:http://blog.csdn.net/heyutao007/article/details/44900649
配置DelegatingFilterProxy的常用方法如下所示:


[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <filter>    
  2.     <filter-name>testFilter</filter-name>    
  3.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
  4.     <init-param>    
  5.         <param-name>targetBeanName</param-name>    
  6.         <param-value>testBean</param-value>    
  7.     </init-param>    
  8. </filter>            
  9.        <filter-mapping>    
  10.     <filter-name>testFilter</filter-name>    
  11.     <url-pattern>/*</url-pattern>    
  12. </filter-mapping>    


含义是有一个过滤器,它指向一个bean,这个bean在spring中的名字为testBean,testBean也必需实现javax.servlet.Filter。
其他可以通过web.xml传递的参数如下:
        (1) contextAttribute,使用委派Bean的范围,其值必须从org.springframework.context.ApplicationContext.WebApplicationContext中取得,默认值是session;
        (2) targetFilterLifecycle,是否调用Filter的init和destroy方法,默认为false。
        

所以DelegationgFilterProxy的全项配置信息如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <filter>    
  2. <filter-name>testFilter</filter-name>    
  3. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
  4. <init-param>    
  5.     <param-name>targetBeanName</param-name>    
  6.     <param-value>testBean</param-value>    
  7. </init-param>    
  8. <init-param>    
  9.     <param-name>contextAttribute</param-name>    
  10.     <param-value>session</param-value>    
  11. </init-param>    
  12. <init-param>    
  13.     <param-name>targetFilterLifecycle</param-name>    
  14.     <param-value>false</param-value>    
  15. </init-param>    
  16. </filter>   
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1.            
  2. <filter-mapping>    
  3. <filter-name>testFilter</filter-name>    
  4. <url-pattern>/*</url-pattern>    
  5. </filter-mapping>    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值