JAVA从入门到精通之DelegatingFilterProxy分析
白羽 2018-07-09 来源 :网络 阅读 896 评论 0

摘要:本文将带你了解JAVA从入门到精通之DelegatingFilterProxy分析,希望本文对大家学JAVA有所帮助。


前言

最近在分析源码时发现了一个配置如下:

#web.xml文件中<filter>
    <filter-name>cacheSessionFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-vale>true</param-value>
    </init-param></filter><filter-mapping>
    <filter-name></filter-name>
    <url-pattern>/*</url-pattern></filter-mapping>
 
#applicationContext.xml文件<bean name="cacheSessionFilter"  class="xxxx.xxx.xxFilter"/>
那么这个DelegatingFilterProxy到底是干嘛用的那???网上查了很多,最终总结为一句话即:DelegateFilterProxy实现了服务器容器(tomcate was等)中的filter调用spring容器中类的功能,那这是如何实现的那?
DelegatingFilterProxy和spring容器如何关联的?
观察web.xml文件,spring的初始化一般都是在非常靠前的,也就是说在使用delegatingfilterproxy之前,spring容器已经初始化完成了,然后观察delegatingfilterproxy的源码会发现有这么一段
protected void initFilterBean() throws ServletException {
        synchronized (this.delegateMonitor) {
            if (this.delegate == null) {
                // If no target bean name specified, use filter name.
                if (this.targetBeanName == null) {
                    this.targetBeanName = getFilterName();
                }
                // Fetch Spring root application context and initialize the delegate early,
                // if possible. If the root application context will be started after this
                // filter proxy, we'll have to resort to lazy initialization.
                WebApplicationContext wac = findWebApplicationContext();
                if (wac != null) {
                    this.delegate = initDelegate(wac);
                }
            }
        }
    }
没错WebApplicationContext其实就是spring容器,也就是说DelegatingFilterProxy中保存有spring的容器,而在WebApplicationContext中有一个和DelegatingFilerProxy同名的类(这个类就是我们自己的类),DelegatingFilterProxy会从WebApplicationContext中寻找那个和其同名的类,然后将所有的动作赋予给它。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
 
        // Lazily initialize the delegate if necessary.
        Filter delegateToUse = this.delegate;
        if (delegateToUse == null) {
            synchronized (this.delegateMonitor) {
                delegateToUse = this.delegate;
                if (delegateToUse == null) {
                    WebApplicationContext wac = findWebApplicationContext();
                    if (wac == null) {
                        throw new IllegalStateException("No WebApplicationContext found: " +
                                "no ContextLoaderListener or DispatcherServlet registered?");
                    }
                    delegateToUse = initDelegate(wac);
                }
                this.delegate = delegateToUse;
            }
        }
 
        // Let the delegate perform the actual doFilter operation.
        invokeDelegate(delegateToUse, request, response, filterChain);
    }
其中的delegateToUse就是从webapplicationcontext中取出来的:
@Override
    protected void initFilterBean() throws ServletException {
        synchronized (this.delegateMonitor) {
            if (this.delegate == null) {
                // If no target bean name specified, use filter name.
                if (this.targetBeanName == null) {
                    this.targetBeanName = getFilterName();
                }
                // Fetch Spring root application context and initialize the delegate early,
                // if possible. If the root application context will be started after this
                // filter proxy, we'll have to resort to lazy initialization.
                WebApplicationContext wac = findWebApplicationContext();
                if (wac != null) {
                    this.delegate = initDelegate(wac);
                }
            }
        }
    }

 

以上就介绍了JAVA的相关知识,希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程