spring拦截器中使用spring的自动注入

  需要在spring的拦截器中使用自定义的服务,这要就设计到将服务注入到拦截器中。网上看的情况有两种:

  1、

 1 @Configuration 
 2 public class OptPermissionHandlerInterceptor extends HandlerInterceptorAdapter { 
 3   private Logger logger = LoggerFactory.getLogger(OptPermissionHandlerInterceptor.class); 
 4 
 5   @Autowired 
 6   private OperatorLogService operatorLogService; //这里使用@Autowired无法注入成功 
 7 
 8   @Override 
 9   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws      Exception { 
10         if (true) { 
11             return true; 
12         } else { 
13             String result = "当前登录用户无权限!"; 
14             response.getOutputStream().write(result.getBytes()); 
15             response.setStatus(HttpStatus.OK.value()); 
16             return false; 
17         } 
18   } 
19 
20   @SuppressWarnings("rawtypes") 
21   @Override 
22   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 
23     try { 
24       if (handler instanceof HandlerMethod) { 
25            HandlerMethod handlerMethod = (HandlerMethod) handler; 
26            String beanName = handlerMethod.getBean().getClass().toString(); 
27            String methodName = handlerMethod.getMethod().getName(); 
28            String uri = request.getRequestURI(); 
29            String remoteAddr = request.getRemoteAddr(); 
30            String sessionId = request.getSession().getId(); 
31 
32            OperatorLog optLog = new OperatorLog(); 
33            optLog.setBeanName(beanName); 
34            optLog.setMethodName(methodName); 
35            optLog.setRemoteAddr(remoteAddr); 
36            optLog.setSessionId(sessionId); 
37            optLog.setUri(uri); 
38 
39       if (operatorLogService == null) {//解决service为null无法注入问题 
40          System.out.println("operatorLogService is null!!!"); 
41          BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); 
42          operatorLogService = (OperatorLogService) factory.getBean("operatorLogService"); 
43       } 
44       operatorLogService.saveOperatorLog(optLog); 
45      } 
46    } catch (Exception e) { 
47        logger.error("", e); 
48    } 
49   } 
50   
51   @Override 
52   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, 
53       Exception ex) throws Exception {
54 
55   } 
56 
57 }

  2、

 1 @Override
 2     public void addInterceptors(InterceptorRegistry registry) {
 3         registry.addInterceptor(sessionInterceptor())
 4                 .addPathPatterns("/**")
 5                 .excludePathPatterns( "/push/**");
 6         super.addInterceptors(registry);
 7     }
 8 
 9 @Bean
10     public SessionInterceptor sessionInterceptor() {
11         return new SessionInterceptor();
12     }

  第二种的方式中 sessionInterceptor类中也可以使用:@Configuration,总之就是需要让spring进行管理。

posted @ 2018-04-13 18:02  ngulc  阅读(2308)  评论(0编辑  收藏  举报