springboot拦截器注入业务对象

springbootlan拦截器注入业务对象

一、问题场景:

1、拦截器需要实现HandlerInterceptor接口;

2、使用WebMvcConfigurer接口用来注册拦截器;

3、因为拦截器是在上下文容器 Spring Context 初始化之前执行,所以没有办法直接在拦截器中注入Service对象,解决办法如下:

二、解决方案:

使用WebApplicationContextUtils类来直接获取bean。

Demo demo = org.springframework.web.context.support.WebApplicationContextUtils
          .getRequiredWebApplicationContext(request.getServletContext())
          .getBean(Demo.class);

 

转载于:https://www.cnblogs.com/kjgym/p/11881672.html

おすすめ

転載: blog.csdn.net/weixin_44351477/article/details/112513153