Spring DelegatingFilterProxy类

DelegatingFilterProxy 从字面意思可以看出它就是个过滤器代理,它本身也间接实现了Servlet规范中的Filter接口。
DelegatingFilterProxy类图

  • 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值