spring攔截器中使用spring的自動注入

  須要在spring的攔截器中使用自定義的服務,這要就設計到將服務注入到攔截器中。網上看的狀況有兩種:spring

  一、session

 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 }

  二、ide

 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進行管理。post

相關文章
相關標籤/搜索