從該類的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的生命週期