(结合自定义注解,实现日志的处理)
1.引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
2.创建自己的自定义注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BussinessLog {
/** 操作类型: 新增 修改 删除 ,赋值要采用枚举,便于后期日志的整理归档 **/
String operateType() default "";
/** 功能模块 来源: 最好采用菜单的唯一性标志,便于的归档 **/
String sourceList() default "";
/** 操作信息 **/
String msg() default "";
}
2.创建切面
@Slf4j
@Aspect
@Component
public class BusinessLogAspect {
//利用注解 配置织入点
@Pointcut("@annotation(com.qinzhy.mydemo.annotation.BussinessLog)")
public void annotationPointCut(){}
/**
* 后置通知
*/
@After("annotationPointCut()")
public void insertLogByAnnotion(JoinPoint joinPoint){
log.info(JSON.toJSONString("后置通知 : "+joinPoint));
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
//获取注解的信息
String operateType = bussinessLog.operateType();
String sourceList = bussinessLog.sourceList();
String msg = bussinessLog.msg();
log.info("后置方法式AOP拦截 【{}】" , operateType);
log.info("后置方法式AOP拦截 【{}】" , sourceList);
log.info("后置方法式AOP拦截 【{}】" , msg);
//获取session中用户的信息
//保存日志信息
}
/**
* 前置通知
*/
@Before("execution(* com.qinzhy.mydemo.controller.TestAspectController.*(..))")
public void beforeLogByAnnotion(JoinPoint joinPoint){
log.info(JSON.toJSONString("前置通知 : "+ joinPoint));
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
//获取注解的信息
String operateType = bussinessLog.operateType();
String sourceList = bussinessLog.sourceList();
String msg = bussinessLog.msg();
log.info("方法式AOP拦截 【{}】" , operateType);
log.info("后置异常注解式AOP拦截 【{}】" , sourceList);
log.info("方法式AOP拦截 【{}】" , msg);
//获取session中用户的信息
//保存日志信息
}
/**
* 异常日志处理
*/
@AfterThrowing("annotationPointCut()")
public void exceptionLogByAnnotion(JoinPoint joinPoint){
log.info(JSON.toJSONString("前置通知 : "+ joinPoint));
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
BussinessLog bussinessLog = method.getAnnotation(BussinessLog.class);
//获取注解的信息
String operateType = bussinessLog.operateType();
String sourceList = bussinessLog.sourceList();
String msg = bussinessLog.msg();
log.info("异常捕获注解式AOP拦截 【{}】" , operateType);
log.info("异常捕获注解式AOP拦截 【{}】" , sourceList);
log.info("异常捕获注解式AOP拦截 【{}】" , msg);
//获取session中用户的信息
//保存日志信息
}
}
3.使用该自定义的日志注解
/**
* 测试利用切面进行日志记录
*/
@BussinessLog(operateType = "add",sourceList = "新增列表", msg = "测试注解式拦截")
@GetMapping("/add")
public String testAspect(){
System.out.println("执行中>>>>>>>>>>>>>");
return "success";
}
4.实际使用案例
异常日志处理 或者成功后日志处理:未加入的安全模块以及session处理
@Slf4j
@Aspect
@Component
public class BusinessLogAspect {
//利用注解 配置织入点
@Pointcut("@annotation(com.qinzhy.mydemo.annotation.BussinessLog)")
public void annotationPointCut(){}
/**
* 后置通知
* @param joinPoint 切点
* @param jsonResult 返回的参数
*/
@AfterReturning( pointcut = "annotationPointCut()", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, Object jsonResult){
log.info(JSON.toJSONString("成功日志处理通知 : "+joinPoint));
handleLog(joinPoint, null, jsonResult);
}
/**
* 返回的异常
* @param joinPoint
* @param e
*/
@AfterThrowing(value = "annotationPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e){
log.info(JSON.toJSONString("异常拦截通知 : "+ joinPoint));
handleLog(joinPoint, e, null);
}
/**
* 日志处理
* @param joinPoint 切点
* @param e 异常
* @param jsonResult 返回的参数
*/
protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult){
//获取注解
BussinessLog bussinessLog = this.getAnnotationLog( joinPoint);
if(bussinessLog == null){
return;
}
//获取注解的信息
String operateType = bussinessLog.operateType();
String sourceList = bussinessLog.sourceList();
String msg = bussinessLog.msg();
log.info("注解式AOP拦截 【{}】" , operateType);
log.info("注解式AOP拦截 【{}】" , sourceList);
log.info("注解式AOP拦截 【{}】" , msg);
//获取用户信息
//获取请求参数信息 ip 等
// 返回参数
if(jsonResult != null){
//赋值到返回参数存放字段里
}
//根据Exception e 是否存在来判断是否执行成果
if ( e != null){
//设置执行失败的状态
//设置失败的原因 e.getMessage();
}else{
//设置执行成功的状态
}
//调用保存到数据库的方法
}
/**
* 获取注解里的信息
* @param joinPoint
* @return BussinessLog
*/
private BussinessLog getAnnotationLog(JoinPoint joinPoint){
joinPoint.getSignature();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
if(method != null){
return method.getAnnotation(BussinessLog.class);
}
return null;
}
}