从该类的uml图入手,了解该类的继承层次,UML图以下spring
从uml图能够看到,该类在springbean 的基础上多实现了filter,能够认为是一个spring管理的filterapp
过滤器 有三个方法 init doFilter destory ,所以首先从init方法入手 。init在其父类 GenericFilterBean 中实现,方法摘要以下:ide
/**
* 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
*/
@Override
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() + "'");
}ui
this.filterConfig = filterConfig;this
// Set bean properties from init parameters.spa
//将filter的配置转换为spring可管理的对象
PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
Environment = this.environment;
if (env == null) {
env = new StandardServletEnvironment();
}
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
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);
}
}.net
// Let subclasses do whatever initialization they like.debug
//调用子类的方法初始化
initFilterBean();代理
if (logger.isDebugEnabled()) {
logger.debug("Filter '" + filterConfig.getFilterName() + "' configured successfully");
}
}orm
DelegatingFilterProxy 类中 initFilterBean()方法实现以下
@Override
protected void initFilterBean() throws ServletException {
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
// If no target bean name specified, use filter name.
//获取代理对象的beanName
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.
//从spring容器中获取代理对象
WebApplicationContext wac = findWebApplicationContext();
if (wac != null) {
//执行代理对象的init方法
this.delegate = initDelegate(wac);
}
}
}
}
再看doFilter方法 源码摘要以下:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// Lazily initialize the delegate if necessary.
Filter delegateToUse = this.delegate;
//代理对象为null 则跟调用初始化方法获取代理对象
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.
//执行代理对象的dofilter方法
invokeDelegate(delegateToUse, request, response, filterChain);
}
因此从源码能够看出,该类只是一个filter代理类,主要是使用spring容器来管理filter的生命周期