使用aop切面编程进行日志处理
1.定义注解;
2.定义切点;
3.进入切面;
4.进行日志处理;
代码:
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogA {
//String dateStr();
// String ids();
// String returnStr();
}
@Slf4j
@Aspect
@Component
public class LogAopService {
/**
* 添加切入点
*/
@Pointcut("@annotation(com.bh.aop.LogA)")
public void checkPreAuthorize() {
}
/**
* 之前操作
*/
/* @Before("checkPreAuthorize()")
public void check(JoinPoint joinPoint) {
System.out.println("经过之前验证");
}
*//**
* 之后操作
*//*
@After("checkPreAuthorize()")
public void check2(JoinPoint joinPoint) {
System.out.println("经过之后验证");
}
*/
/**
* 处理完请求后执行
*/
@AfterReturning(pointcut = "@annotation(com.bh.aop.LogA)",returning = "returnV")
public void doAfterReturning(JoinPoint joinPoint,Object returnV) {
Object[] args = joinPoint.getArgs();
String s = JSONObject.toJSONString(args);
log.info("【LogAop】调用方法:{},请求参数:{},返回结果:{}",joinPoint.getSignature().getName(),s,returnV);
}
/**
* 拦截异常操作
*/
@AfterThrowing(value = "@annotation(com.bh.aop.LogA)")
public void doAfterThrowing(JoinPoint joinPoint) {
System.out.println("处理发生异常时");
}
}
@LogA
@Override
public String postLetterResToTrs(JSONObject object){}