码迷,mamicode.com
首页 > 其他好文 > 详细

MethodInterceptor方法拦截器

时间:2015-06-04 15:15:36      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:

1.自定义一个annotation

 

  1. package com.websystem.util;  
  2.   
  3. import java.lang.annotation.Documented;  
  4. import java.lang.annotation.ElementType;  
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.RetentionPolicy;  
  7. import java.lang.annotation.Target;  
  8.   
  9. /** 
  10.  * des:自定义使方法跳过拦截的注解 
  11.  * author: zbl 
  12.  * date: 2014年9月3日 
  13.  **/  
  14. @Target(ElementType.METHOD)  
  15. @Retention(RetentionPolicy.RUNTIME)  
  16. @Documented  
  17. public abstract @interface RequiredInterceptor{  
  18.     boolean required() default true;  
  19. }  

2.在Controller里面的方法使用自定义的annotation,下面是一个登进登出的例子。

 

 

  1. package com.websystem.controller;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.springframework.beans.factory.annotation.Value;  
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.ui.ModelMap;  
  11. import org.springframework.web.bind.annotation.ModelAttribute;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.RequestMethod;  
  14. import org.springframework.web.bind.annotation.ResponseBody;  
  15. import org.springframework.web.bind.annotation.SessionAttributes;  
  16. import org.springframework.web.bind.support.SessionStatus;  
  17. import org.springframework.web.servlet.ModelAndView;  
  18. import org.springframework.web.servlet.view.RedirectView;  
  19.   
  20. import com.websystem.model.ManagerModel;  
  21. import com.websystem.service.ManagerService;  
  22. import com.websystem.util.AESPlusHelper;  
  23. import com.websystem.util.Constant;  
  24. import com.websystem.util.RequiredInterceptor;  
  25.   
  26. /** 
  27.  * des: 
  28.  * author: zbl 
  29.  * date: 2014年8月26日 
  30.  **/  
  31. @Controller  
  32. @SessionAttributes(Constant.Manager)  
  33. public class ManagerController {  
  34.   
  35.     @Resource  
  36.     private ManagerService managerServiceImpl;  
  37.   
  38.     @RequiredInterceptor(required = false)  
  39.     @RequestMapping(value = "manager/login.do",method = RequestMethod.GET)    
  40.     public ModelAndView login(ManagerModel managerModel,ModelMap model){  
  41.         ManagerModel manager = managerServiceImpl.getManager(managerModel);  
  42.           
  43.         if(manager!=null){  
  44.             manager.setPassword("");  
  45.             model.addAttribute(Constant.Manager, manager);  
  46.             return new ModelAndView(new RedirectView(Constant.MainURL));  
  47.         }else{  
  48.             return new ModelAndView(new RedirectView(Constant.LoginURL));  
  49.         }  
  50.     }  
  51.       
  52.     @RequiredInterceptor(required = false)  
  53.     @RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)  
  54.     @ResponseBody  
  55.     public Object logout(SessionStatus status){  
  56.         status.setComplete();  
  57.           
  58.         Map<String,Object> map = new HashMap<String,Object>();  
  59.         map.put(Constant.Success, true);  
  60.         return map;  
  61.     }  
  62. }  


3.定义MethodInterceptor,里面可以处理AOP逻辑代码。

 

 

  1. package com.websystem.util;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5. import org.springframework.core.annotation.AnnotationUtils;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. /** 
  9.  * des: 
  10.  * author: zbl 
  11.  * date: 2014年9月3日 
  12.  **/  
  13. @Component  
  14. public class SessionInterceptor implements MethodInterceptor {  
  15.   
  16.     @Override  
  17.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  18.         // TODO Auto-generated method stub   
  19.         RequiredInterceptor requiredInterceptor = AnnotationUtils.findAnnotation(invocation.getMethod(), RequiredInterceptor.class);  
  20.         if(requiredInterceptor!=null){  
  21.             System.out.println(invocation.getMethod().getName());  
  22.             //你要做的逻辑代码   
  23.         }  
  24.         return invocation.proceed();  
  25.     }  
  26.   
  27. }  

4.添加配置

 

 

  1. <context:component-scan base-package="com.websystem.controller,com.websystem.*.impl,com.websystem.util"/>  
  2.       
  3.     <mvc:annotation-driven/>  
  4.       
  5.     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  6.         <property name="beanNames">  
  7.             <list><value>*Controller</value></list>  
  8.         </property>  
  9.         <property name="interceptorNames">  
  10.             <list><value>sessionInterceptor</value></list>  
  11.         </property>  
  12.     </bean>  

 

这里用的是所有Controller过滤一遍,也可以定位为service 和dao  。 也可以用这个MethodInterceptor做缓存。

MethodInterceptor方法拦截器

标签:

原文地址:http://www.cnblogs.com/hailei/p/4551581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!