Spring MVC过滤器-超类 GenericFilterBean
GenericFilterBean
抽象类GenericFilterBean实现了javax.servlet.Filter、org.springframework.beans.factory.BeanNameAware、org.springframework.context.EnvironmentAware、org.springframework.web.context.ServletContextAware、org.springframework.beans.factory.InitializingBean和org.springframework.beans.factory.DisposableBean五个接口,作用如下:(1) Filter,实现过滤器;(2) BeanNameAware,实现该接口的setBeanName方法,便于Bean管理器生成Bean;(3) EnvironmentAware,实现该接口的setEnvironment方法,指明该Bean运行的环境;(4) ServletContextAware,实现该接口的setServletContextAware方法,指明上下文;(5) InitializingBean,实现该接口的afterPropertiesSet方法,指明设置属性生的操作;(6) DisposableBean,实现该接口的destroy方法,用于回收资源。GenericFilterBean的工作流程是:init-doFilter-destory,其中的init和destory在该类中实现,doFilter在具体实现类中实现。init的代码如下:
/**
* Standard way of initializing this filter.
* Map config parameters onto bean properties of this filter, and
* invoke subclass initialization.
* @param filterConfig the configuration for this filter
* @throws ServletException if bean properties are invalid (or required
* properties are missing), or if subclass initialization fails.
* @see #initFilterBean
*/
public final void init(FilterConfig filterConfig) throws ServletException {
Assert.notNull(filterConfig, “FilterConfig must not be null”);
if (logger.isDebugEnabled()) {
logger.debug(“Initializing filter '” + filterConfig.getFilterName() + “’”);
}
this.filterConfig = filterConfig; // Set bean properties from init parameters. try { PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment)); initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage(); logger.error(msg, ex); throw new NestedServletException(msg, ex); } // Let subclasses do whatever initialization they like. initFilterBean(); if (logger.isDebugEnabled()) { logger.debug("Filter '" + filterConfig.getFilterName() + "' configured successfully"); }
}
该方法来自于javax.servlet.Filter,即过滤器的初始化,它的主要工作集中于以下几行代码:
// 从properties文件中获取值,这里是web.xml
PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
// 设置bean适配器
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
// 设置上下文,这里的servletContext的设定继承自ServletContextAware的setter
ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
// 将上下文信息和环境信息设置到bean适配器中,这里的environment来自于EnvironmentAware的setter
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
// 初始化bean适配器
initBeanWrapper(bw);
// 将从properties中获取的资源放置到bean适配器
bw.setPropertyValues(pvs, true);
// 初始化bean
initFilterBean();
其中initFilterBean方法在两个位置起作用,一处是上文所述的init方法,另一处是afterPropertiesSet方法,在调用该方法前,需要保证用于Filter的所有的bean都已被设置,该方法由子类实现。
GenericFilterBean中包含一个内部私有类FilterConfigPropertyValues,主要用于将web.xml中定义的init-param的值取出。
Spring MVC过滤器-超类 GenericFilterBean相关推荐
- Spring MVC过滤器-超类
Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所当然地实现了javax.servlet.Filter,不过实现的方式有以下几类 ...
- Spring MVC过滤器-HttpPutFormContentFilter
在Spring MVC过滤器-HiddenHttpMethodFilter中我们提到,jsp或者说html中的form的method值只能为post或get,我们可以通过HiddenHttpMetho ...
- Spring MVC过滤器-HiddenHttpMethodFilter
随时随地技术实战干货,获取项目源码.学习资料,请关注源代码社区公众号(ydmsq666) from:Spring MVC过滤器-HiddenHttpMethodFilter_Blake Luo-CSD ...
- Spring MVC过滤器-委派过滤器代理(DelegatingFilterProxy)
org.springframework.web.filter中有一个特殊的类--DelegatingFilterProxy,该类其实并不能说是一个过滤器,它的原型是FilterToBeanProxy, ...
- Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
Spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Charact ...
- Spring MVC过滤器-登录过滤
以下代码是继承OncePerRequestFilter实现登录过滤的代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- Spring MVC过滤器-RequestContextFilter
在Spring中可以使用很多种方式来实现request的转发,将页面提交的request转发到Controller中,而实现这一功能的原理即是设置LocaleContextHolder和Request ...
- spring mvc 过滤器
1.什么是过滤器 a. 过滤器是处于客户端与服务器资源文件之间的一道过滤网,在访问资源文件之前,通过一系列的过滤器对请求进行修改.判断等,把不符合规则的请求在中途拦截或修改.也可以对响应进行过滤,拦截 ...
- Spring MVC过滤器 登陆过滤
代码: package com.uniquedu.filter;import java.io.IOException; import java.io.PrintWriter;import javax. ...
最新文章
- SAP MM 初阶之事务代码MIGO中采购退货的处理
- 【Android 文件管理】分区存储 ( 分区存储机制 和 文件索引数据 )
- 修改默认的pip版本为对应python2.7
- 《Python Cookbook 3rd》笔记(4.15):顺序迭代合并后的排序迭代对象
- MIUI V5正式发布 全部功能展示PPT回看
- 【AI研究报告】世界顶级投资银行高盛的AI报告:中国人工智能的现状及创新市场...
- 玩转SpringBoot 2.x 解析BeanPostProcessor原理篇
- UVA 11237 - Halloween treats(鸽笼原理)
- 怎么在cmd中安装python库_cmd中安装python库时出现的错误
- tcs标准编写软件_tcs2010(中国标准编写模板)免费版
- arcgis两点之间连线_three3D地图设置两点之间的连线
- 微信小程序 选择器 picker ;demo大全附效果图
- 1 error and 0 warnings potentially fixable with the `--fix` optio
- 华为手机主界面的返回键怎么调出来_华为手机没有返回键怎么调出来
- 批量移动文件夹到对应文件夹
- source insight的使用
- 语音怎么在线转文字,语音转文字的小技巧
- jQuery动画函数
- 人脸检测-ROI与泛洪填充(5)
- linux上最好的p图软件,10款好用的手机P图软件排行榜