Spring MVC过滤器-委派过滤器代理(DelegatingFilterProxy)

org.springframework.web.filter中有一个特殊的类——DelegatingFilterProxy,该类其实并不能说是一个过滤器,它的原型是FilterToBeanProxy,即将Filter作为spring的bean,由spring来管理。

配置方式如下:

1.配置web.xml

方法1:(不要求filer-name与实现javax.servlet.Filter的bean的类名相同,只要将targetBeanName的value值设置为相应的bean的类名即可),但是这种方法不会执行filter的init()和destory()方法。

    <filter>
        <filter-name>testFilter2filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>  
            <param-name>targetBeanNameparam-name>  
            <param-value>testFilter2param-value>  
        init-param>  
    filter>

    <filter-mapping>
        <filter-name>testFilter2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

方法2(要求filer-name与实现javax.servlet.Filter的bean的类名相同,但是这种方法不会执行filter的init()和destory()方法。):

    <filter>
        <filter-name>testFilter2filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        param> 

    <filter-mapping>
        <filter-name>testFilter2filter-name>
        /*
    

当然,也可以同时加上targetFilterLifecycle和targetBeanName参数,即:

    <filter>
        <filter-name>filter-namefilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        param> 
        param>  
            <param-name>targetBeanNameparam-name>  
            <param-value>testFilter2param-value>  
        param>  
    filter>

    <filter-mapping>
        <filter-name>filter-namefilter-name>
        /*
    

2、实现javax.servlet.Filter并且在applicationContext.xml中配置bean

关于applicationContext.xml,可以查看:http://blog.csdn.net/zuoyexingchennn/article/details/50426869

你可能感兴趣的:(Java语言,框架)