MethodInterceptor拦截器
来源:互联网 发布:即时沟通软件 编辑:程序博客网 时间:2024/05/23 13:55
1.自定义一个annotation
package com.websystem.util;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * des:自定义使方法跳过拦截的注解 * author: zbl * date: 2014年9月3日 **/@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic abstract @interface RequiredInterceptor{boolean required() default true;}
2.在Controller里面的方法使用自定义的annotation,下面是一个登进登出的例子。
package com.websystem.controller;import java.util.HashMap;import java.util.Map;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.SessionAttributes;import org.springframework.web.bind.support.SessionStatus;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.RedirectView;import com.websystem.model.ManagerModel;import com.websystem.service.ManagerService;import com.websystem.util.AESPlusHelper;import com.websystem.util.Constant;import com.websystem.util.RequiredInterceptor;/** * des: * author: zbl * date: 2014年8月26日 **/@Controller@SessionAttributes(Constant.Manager)public class ManagerController {@Resourceprivate ManagerService managerServiceImpl;@RequiredInterceptor(required = false)@RequestMapping(value = "manager/login.do",method = RequestMethod.GET) public ModelAndView login(ManagerModel managerModel,ModelMap model){ManagerModel manager = managerServiceImpl.getManager(managerModel);if(manager!=null){manager.setPassword("");model.addAttribute(Constant.Manager, manager);return new ModelAndView(new RedirectView(Constant.MainURL));}else{return new ModelAndView(new RedirectView(Constant.LoginURL));}}@RequiredInterceptor(required = false)@RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)@ResponseBodypublic Object logout(SessionStatus status){status.setComplete();Map<String,Object> map = new HashMap<String,Object>();map.put(Constant.Success, true);return map;}}
3.定义MethodInterceptor,里面可以处理AOP逻辑代码。
package com.websystem.util;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.springframework.core.annotation.AnnotationUtils;import org.springframework.stereotype.Component;/** * des: * author: zbl * date: 2014年9月3日 **/@Componentpublic class SessionInterceptor implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {// TODO Auto-generated method stubRequiredInterceptor requiredInterceptor = AnnotationUtils.findAnnotation(invocation.getMethod(), RequiredInterceptor.class);if(requiredInterceptor!=null){System.out.println(invocation.getMethod().getName());//你要做的逻辑代码}return invocation.proceed();}}4.添加配置
<context:component-scan base-package="com.websystem.controller,com.websystem.*.impl,com.websystem.util"/> <mvc:annotation-driven/><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames"><list><value>*Controller</value></list></property><property name="interceptorNames"><list><value>sessionInterceptor</value></list></property></bean>
总结:不废话解析那么多,直接上成功的例子。
0 2
- MethodInterceptor拦截器
- Spring方法拦截器MethodInterceptor
- Spring方法拦截器MethodInterceptor
- Spring方法拦截器MethodInterceptor
- Spring方法拦截器MethodInterceptor
- SpringAOP MethodInterceptor方法拦截器
- Spring方法拦截器MethodInterceptor
- Spring方法拦截器MethodInterceptor
- spring 拦截器 MethodInterceptor 配置 config aop
- 方法拦截器(MethodInterceptor)使用
- spring 拦截器 MethodInterceptor 配置 config aop
- spring 方法拦截器 MethodInterceptor接口
- spring 拦截器 MethodInterceptor 配置 config aop
- spring aop 拦截器 MethodInterceptor 配置
- MethodInterceptor拦截器 加注解精准拦截method
- Spring AOP: Spring之面向方面编程 拦截器 MethodInterceptor
- 使用方法拦截器MethodInterceptor和AOP统一处理log
- Spring方法拦截器MethodInterceptor和AOP统一处理log
- 书单
- 有关内存的思考题
- EEPlat订单管理系统开发(四)之__流程的简单应用
- COM学习笔记(五):CoCreateInstance
- awk -F 分隔符的正则表达式
- MethodInterceptor拦截器
- 使用ViewPager实现左右循环滑动及滑动跳转
- haojiulahiugu
- 链表反转(递归与非递归实现)
- Process name from its pid in linux
- PHP中序列号和反序列化
- 关于java实现插入排序的思考
- ubuntu 13.10搭建MTK android源码编译环境
- ibatis报错 There is no statement named userList in this SqlMap.