已经解决 spring boot 拦截器中注入bean

来源:互联网 发布:python与weka的区别 编辑:程序博客网 时间:2024/06/08 06:31

已经解决 spring boot 拦截器中如何注入bean的问题


@Configurationpublic class MyWebAppConfigurer extends WebMvcConfigurerAdapter

@BeanRequestInterceptor requestInterceptor() {    return new RequestInterceptor();}


@Componentpublic class RequestInterceptor implements HandlerInterceptor

@Autowiredprivate ApplicationConfiguration configuration;

// 多个拦截器组成一个拦截器链// addPathPatterns 用于添加拦截规则// excludePathPatterns 用户排除拦截@Overridepublic void addInterceptors(InterceptorRegistry registry) {    //时间拦截器    registry.addInterceptor(requestExecuteTimeInterceptor()).addPathPatterns("/**");    //API请求拦截器    registry.addInterceptor(requestInterceptor()).addPathPatterns("/**").excludePathPatterns("/weixin/server");    super.addInterceptors(registry);}

1 0