如何获取spring mvc中的拦截器的bean

[b]举个例子Test2Interceptor要获取TestInterceptor来操作[/b]

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.xxx.TestInterceptor">
</bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.xxx.Test2Interceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>


public class TestInterceptor implements HandlerInterceptor {

}


public class Test2Interceptor implements HandlerInterceptor,ApplicationContextAware {
private ApplicationContext applicationContext;
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object object) throws Exception {
Map<String,MappedInterceptor> map = applicationContext.getBeansOfType(MappedInterceptor.class);
for(MappedInterceptor m : map.values()) {
if(m.getInterceptor() instanceof TestInterceptor) {
System.out.println(m.getInterceptor());
}
}
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;

}
}

得出原来spring mvc把<mvc:interceptors>配置的bean放到MappedInterceptor实例中
咱们看下MappedInterceptor源码

public final class MappedInterceptor {

private final String[] includePatterns;

private final String[] excludePatterns;

private final HandlerInterceptor interceptor;
//省略
}

这里得出MappedInterceptor 持有HandlerInterceptor实例对象,HandlerInterceptor也就是我们刚刚写的拦截器的要实践的接口。
这样我们就明白了,大功告成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值