您的位置:首页 > 编程语言 > Java开发

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时已经添加。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: