spring filter 过滤器 注入 对象 属性

配置一:

Java代码   收藏代码
  1. <filter>  
  2.         <filter-name>DelegatingFilterProxy</filter-name>  
  3.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  4.         <init-param>  
  5.             <param-name>targetBeanName</param-name>  
  6.             <param-value>myFilter</param-value>         //自己过滤器的名字  
  7.         </init-param>  
  8.         <init-param>  
  9.             <param-name>targetFilterLifecycle</param-name>  
  10.             <param-value>true</param-value>  
  11.         </init-param>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>DelegatingFilterProxy</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  

 

配置二:

Java代码   收藏代码
  1. <filter>  
  2.         <filter-name>myFilter</filter-name>  
  3.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  4.         <init-param>  
  5.             <param-name>targetFilterLifecycle</param-name>  
  6.             <param-value>true</param-value>  
  7.         </init-param>  
  8.     </filter>  
  9.   
  10.     <filter-mapping>  
  11.         <filter-name>DelegatingFilterProxy</filter-name>  
  12.         <url-pattern>/*</url-pattern>  
  13.     </filter-mapping>  

 

方法一或者二不同的地方就是在web.xml中的写法不同而已没有太大的区别,配完web.xml之后还要配置 applicationContext.xml中的bean。

applicationContext.xml配置:

Java代码   收藏代码
  1. <bean id="myFilter" class="com.MyFilter"//指名具体的filter类  
  2.     <property name="service">                    //需要注入的具体参数  
  3.         <ref bean="service"/>  
  4.     </property>  
  5. </bean>   

 

 

如果使用注解:

Java代码   收藏代码
  1. @Component("tjFilter")   
  2. public class TjFilter implements Filter {  
  3.   
  4.     @Resource(name="historyManager")  
  5.     private HistoryManager historyManager;  
  6.   
  7.     public void destroy() {  
  8.         // TODO Auto-generated method stub  
  9.   
  10.     }  
  11.    .....  
  12. }  

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值