5、DelegatingFilterProxy源码分析

DelegatingFilterProxy源码分析
复制代码

DelegatingFilterProxyhtml

org.springframework.web.filter.DelegatingFilterProxyweb

在这里插入图片描述

能够看出 DelegatingFilterProxy 类继承 GenericFilterBean,间接实现了Filter这个接口,故而该类属于一个过滤器。那么就会有实现Filter中 init、doFilter、destroy三个方法。spring

filter简介:www.cnblogs.com/xdp-gacl/p/…markdown

GenericFilterBean中实现 filter中 init方法: 在这里插入图片描述 最后调用了app

this.initFilterBean();oop

protected void initFilterBean() throws ServletException {
    }

复制代码

如今盯住 initFilterBean() 就ok了源码分析

public class DelegatingFilterProxy extends GenericFilterBean {
    private String contextAttribute;
    private WebApplicationContext webApplicationContext;
    private String targetBeanName;
    private boolean targetFilterLifecycle;
    private volatile Filter delegate;
    private final Object delegateMonitor;
     ...
      protected void initFilterBean() throws ServletException {
        Object var1 = this.delegateMonitor;
        synchronized(this.delegateMonitor) {
            if(this.delegate == null) {
                if(this.targetBeanName == null) {
                    this.targetBeanName = this.getFilterName();
                }

                WebApplicationContext wac = this.findWebApplicationContext();
                if(wac != null) {
                    this.delegate = this.initDelegate(wac);
                }
            }

        }
    }
   ...
   }
复制代码

在DelegatingFilterProxy 中继承GenericFilterBean 重写initFilterBean(),如今就能够解释 若是不想用 filter-name: shiroFilter 也能够用 targerBeanName 的value 这句话了ui

  1. 在spring容器中去找与 filter-name 相同名字的 bean实例 若是没有设置 targetBeanName 他会默认去加载 filter-name 名字同样的实例initFilterBean()

一、找到被代理类在spring中配置的id并赋值给targetBeanName。this

二、使用找到的id从spring容器中找到具体被代理的类,并赋值给delegateurl

<!--Shiro过滤器-->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>shiroFilter22</param-value>
    </init-param>
</filter>
复制代码

DelegatingFilterProxy
private String targetBeanName; private boolean targetFilterLifecycle; private volatile Filter delegate;

在这里插入图片描述

getFilterName()该方法的做用是,获取被代理的filter在spring中配置的id

在这里插入图片描述

private volatile Filter delegate; 获取过滤器

在这里插入图片描述

initDelegate()该方法的做用是,从spring容器中获取到具体被代理的filter 

在这里插入图片描述

执行doFilter() 在这里插入图片描述

invokeDelegate方法的做用就是执行被代理filter的doFilter方法

在这里插入图片描述

private boolean targetFilterLifecycle; 默认为false 判断targetFilterLifecycle属性是false仍是true,决定是否调用自定义类的init()、destry()方法

相关文章
相关标签/搜索