java SpringMVC+Spring+mybatis自定义aop拦截器实现入参出参控制
2016-10-08 21:41
441 查看
代码下载地址:https://github.com/zhangtianbao2016/demo-parent/tree/develop/my-aop-demo
第一步:自定义拦截器类
package com.omnipotent.interceptor; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject; /** * @desc 自定义拦截器,统一验证处理出入参 * * @author zhangtb * @date 2016年10月5日上午9:11:23 * @since 1.0.0 * @version 1.0.0 */ public class MyAspect { // 日志 private static Logger LOGGER = LoggerFactory.getLogger(MyAspect.class); public Object check(ProceedingJoinPoint pjd) throws Throwable{ Object o = null; try { LOGGER.info("MyAspect.check()..."); HttpServletRequest request = (HttpServletRequest) pjd.getArgs()[0]; // 获取请求参数 String requestParam = request.getParameter("json"); LOGGER.info("requestParam = {}", requestParam); o = pjd.proceed();// 执行controller String result = o.toString(); LOGGER.info("controller返回结果:{}", result); // TODO 记录接口日志 LOGGER.info("记录接口日志..."); // 对返回结果进行处理 JSONObject object = JSONObject.parseObject(result); object.put("message", "请求成功.");// 添加新数据 return object; } catch (Exception e) { e.printStackTrace(); } return o; } }
第二步:在springMVC配置文件添加配置信息
dispatcher-servlet.xml如下<!-- 引入自定义aop配置 --> <import resource="classpath:springMVC-aop.xml"/>
springMVC-aop.xml如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- http://blog.csdn.net/tianjun2012/article/details/47809739 注意:这里是个坑,应该配置在SpringMVC的配置文件中,不然自定义拦截器方法请求进不去!!! proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 自定义拦截器 --> <bean id="myAspect" class="com.omnipotent.interceptor.MyAspect"></bean> <!-- AOP代理设置 --> <!-- 注意:proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。 如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。 如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。 <aop:config proxy-target-class="true"> --> <aop:config> <aop:aspect id="webAspect" ref="myAspect"> <aop:pointcut id="web" expression="execution(* com.omnipotent.controller.*.*(..))" /> <!-- 通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around). --> <aop:around pointcut-ref="web" method="check"/> </aop:aspect> </aop:config> </beans>
第三步:需要依赖的jar包
<dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency>
其他依赖jar包在导入Spring+SpringMVC时已经添加。
相关文章推荐
- 模拟实现Struts拦截器-蕴含着代理模式,AOP,工厂模式,依赖注入,Java 反射,动态构造等机制
- 【Java基础】动态代理实现AOP之控制事务
- PHP简单拦截器实现方法【参考java的AOP】
- 使用拦截器或者AOP实现权限管理(OA系统中实现权限控制)
- 从零开始学 Java - Spring AOP 拦截器的基本实现
- 从零开始写javaweb框架笔记23-使框架具备AOP特性-实现事务控制特性
- AOP系列之三:用Java动态代理实现AOP [zz]
- [转]AOP及其Java实现机制
- Java初学者如何迈出AOP第一步--使用Java 动态代理实现AOP(转)
- Java初学者如何迈出AOP第一步--使用Java 动态代理实现AOP
- AOP下的权限控制实现
- AOP系列之三:用Java动态代理实现AOP
- AOP与权限控制实现
- 通过cglib实现AOP 拦截器
- Java动态代理实现AOP
- 通过Java实现一种常用的权限控制算法
- 推荐一个关于AOP的AOP@WORK的专栏作者的AOP专题系列文章,主要关于Java下的AOP实现,毕竟Java下的AOP实现确实要比.Net下成熟得多,共12篇
- 迈出AOP第一步--使用Java 动态代理实现AOP[zz]
- AOP与权限控制实现(转自板桥里人)
- 用Java动态代理实现AOP