AOP 是基于spring , 通过aspect注解实现:可以获取访问的类 方法 以及参数值. 但是无法获取http原始的请求与相应的对象
AOP获取的请求参数 是基于controller 的参数的,如果 请求中多了一个参数,controller中没有这个参数,那么AOP中肯定也获取不到。
定义切面:
package com.test.aop;
import java.util.Date;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.test.TestService;
import lombok.extern.slf4j.Slf4j;
/**
* 视频播放的切面
* @author LZHH_2008
* @time 2020年6月01日下午5:09:26
*/
@Aspect //使之成为切面类
@Component //把切面类加入到IOC容器中
@Slf4j
public class VideoAspect {
@Autowired
private TestService testService ;
@Autowired
private KmediaPlayRecodLogService kmediaPlayRecodLogService;
/**
*
* 定义切入点
* void
*/
@Pointcut("execution(public * com.test.controller.VideoPlayRecordController.*(..)))")
public void VideoAspect(){}
/**
* @description 在连接点执行之前执行的通知
*/
@Before("VideoAspect()")
public void doBeforeGame(JoinPoint point){
//StringBuilder sb = new StringBuilder();
//sb.append(point.getTarget().getClass().getName());
//sb.append(".");
//sb.append(point.getSignature().getName());
//String methodName = sb.toString();
//处理 saveMessage方法
if("saveMessage".equalsIgnoreCase(point.getSignature().getName())){
String jsonString = JSON.toJSONString(point.getArgs()[0]);
log.info("######切面插入/更新 请求数据为:{}", jsonString);
JSONArray jsonArray = JSON.parseArray(jsonString);
for(int i =0 ; i < jsonArray.size(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String devGbId = jsonObject.getString("devGbId");
String playId = jsonObject.getString("playId");
String user = jsonObject.getString("user");
String videoType = jsonObject.getString("videoType");
String sessionId = jsonObject.getString("sessionId");
//查询是否是插入
//可以做 具体的业务逻辑, 验证签名,添加日志等
}
}
}
}
/**
* @description 在连接点执行之后执行的通知(返回通知和异常通知的异常)
*/
@After("VideoAspect()")
public void doAfterPlay(){
//插入之后
}
/**
* @description 在连接点执行之后执行的通知(返回通知)
*/
@AfterReturning("VideoAspect()")
public void doAfterReturningGame(){
//返回
}
/**
* @description 在连接点执行之后执行的通知(异常通知)
*/
@AfterThrowing("VideoAspect()")
public void doAfterThrowingGame(){
//异常
}
}