DelegatingFilterProxy 从字面意思可以看出它就是个过滤器代理,它本身也间接实现了Servlet规范中的Filter接口。
- Aware: 察觉的、注意到的、感知的,XxAware 就是对xx感知,没有定义任何方法只是一个标识接口
- ApplicationContextAware: 感知应用上下文
- ServletContextAware:感知Servlet上下文
- EnvironmentAware:感知当前应用程序运行的环境
- BeanNameAware:感知当前Bean在应用上下文中的名称
- InitializingBean:初始化完bean时回调,@PostConstruct、init-method同效
- DisposableBean:销毁bean时回调,@PreDestroy、destroy-method同效
@Override
protected void initFilterBean() throws ServletException {
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
// 如果没有指定目标bean名称,则使用筛选器名称。
if (this.targetBeanName == null) {
this.targetBeanName = getFilterName();
}
WebApplicationContext wac = findWebApplicationContext();
if (wac != null) {
this.delegate = initDelegate(wac);
}
}
}
}
// DelegatingFilterProxy#initDelegate从spring上下文中找到委托的过滤器,如果需要时并进行初始化
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
if (isTargetFilterLifecycle()) {
delegate.init(getFilterConfig());
}
return delegate;
}
// DelegatingFilterProxy#invokeDelegate 调用委托过滤器
protected void invokeDelegate(
Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
delegate.doFilter(request, response, filterChain);
}
As of Spring 3.1, {@code DelegatingFilterProxy} has been updated to optionally accept
constructor parameters when using Servlet 3.0’s instance-based filter registration
methods, usually in conjunction with Spring 3.1’s
{@link org.springframework.web.WebApplicationInitializer} SPI. These constructors allow
for providing the delegate Filter bean directly, or providing the application context
and bean name to fetch, avoiding the need to look up the application context from the
ServletContext.
上面的意思大概是Spring3.1+因为Servlet 3.0规范,我们可以使用SPI的方式来注册Filter