springboot实现基于aop的切面日志

作者:yhmoling 时间:2022-09-09 11:53:11 

本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下

通过aop的切面方式实现日志

通切面拦截所有指定包下的所有方法

@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect1{
? ? Logger logger = LoggerFactory.getLogger(LogAspect.class);

? ??
/**
?* 拦截切点
?*/

? ? @Pointcut("execution(*xx.xx.controller.*.*(..))")
? ? private void logPointCut() {
? ? ? ? logger.info("进入注解拦截");

? ? }

? ? //前置通知,在方法之前通知
? ? @Before(value = "logPointCut()")
? ? public void before(JoinPoint jp) {
? ? ? ? logger.info("方法调用前:" + "类名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //后置通知
? ? @After(value = "logPointCut()")
? ? public void doAfter(JoinPoint jp) {
? ? ? ? logger.info("方法调用结束:" + "类名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //环绕通知
? ? @Around("logPointCut()")
? ? public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
? ? ? ? logger.info("方法开始调用》》》》》》");

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法调用结束》》》》》》");
? ? ? ? return retVal;
? ? }

? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()")
? ? public void doAfterReturning(JoinPoint jp) {
? ? ? ? logger.info("写入日志");

? ? }

? ? //异常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法异常,异常信息:" + ex.getMessage());
? ? }
}

拦截自定义注解

定义一个日志注解,在所有要需要记录的方法上加盖注解即可被后续的aop拦截处理

代码如下

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

? ? /**
? ? ?* 日志主题
? ? ?*/
? ? public String title() default "";

? ? /**
? ? ?* 操作具体业务
? ? ?*/
? ? public String business() default "";

? ? /**
? ? ?* 是否保留请求参数
? ? ?*/
? ? public boolean isSaveRequestData() default false;
? ? /**
? ? * 日志的类别,主要用于日志的分开记录和查询
? ? **/
LogType logType() default LogType.LOGIN;
}

拦截切面的实现

@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect {
? ? Logger logger = LoggerFactory.getLogger(LogAspect.class);
? ? @Autowired
? ? private ServiceDemo serviceDemo;

? ? /**
? ? ?* 拦截切点
? ? ?*/

? ? @Pointcut("@annotation(moling.evolution.demo.aop.annotation.Log)")
? ? private void logPointCut() {
? ? ??

? ? }

? ? //前置通知,在方法之前通知
? ? @Before(value = "logPointCut()")
? ? public void before(JoinPoint jp) {
? ? ? ? logger.info("方法调用前:" + "类名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //后置通知
? ? @After(value = "logPointCut()")
? ? public void doAfter(JoinPoint jp) {
? ? ? ? logger.info("方法参数:{}", jp.getArgs());
? ? ? ? logger.info(" ?{} || {}", jp.getStaticPart().getId(), jp.getStaticPart().getSourceLocation());
? ? ? ? jp.getStaticPart().getId();

? ? ? ? logger.info("方法调用结束:" + "类名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //环绕通知
? ? @Around("logPointCut()")
? ? public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
? ? ? ? logger.info("方法开始调用》》》》》》");

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法调用结束》》》》》》");
? ? ? ? return retVal;
? ? }

? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()", returning = "object")
? ? public void doAfterReturning(JoinPoint jp, Object object) {
? ? ? ? System.out.println("返回通知");
? ? ? ? Log log = null;
? ? ? ? try {
? ? ? ? ? ? log = getAnnotationLog(jp);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();

? ? ? ? }
? ? ? ? System.out.println(object);
? ? ? ? System.out.println(log);
? ? ? ? if (log != null) {
? ? ? ? ? ? logger.info(log.title());
? ? ? ? ? ? logger.info(log.business());
? ? ? ? ? ? logger.info(log.user());
? ? ? ? ? ? logger.info(log.isSaveRequestData() + "");
? ? ? ? } else {
? ? ? ? ? ? logger.error("获取注解信息失败");
? ? ? ? }
? ? ? ? serviceDemo.demo();

? ? }

? ? //异常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法异常,异常信息:" + ex.getMessage());
? ? ? ? serviceDemo.error();
? ? }

? ? /**
? ? ?* 是否存在注解,如果存在就获取
? ? ?*/

? ? private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
? ? ? ? Signature signature = joinPoint.getSignature();
? ? ? ? MethodSignature methodSignature = (MethodSignature) signature;
? ? ? ? Method method = methodSignature.getMethod();
? ? ? ? if (method != null) {
? ? ? ? ? ? return method.getAnnotation(Log.class);
? ? ? ? }
? ? ? ? return null;
? ? }
}

基于事件通知实现日志的记录

  • 拦截切面的实现

@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspectContext {

? ? Logger logger = LoggerFactory.getLogger(LogAspectContext.class);
? ? @Autowired
? ? private ApplicationContext applicationContext;

? ? /**
? ? ?* 拦截切点
? ? ?*/
? ? @Pointcut("@annotation(moling.evolution.demo.aop.annotation.Log)")
? ? private void logPointCut() {
? ? ? ? logger.info("进入注解拦截");

? ? }

? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()", returning = "object")
? ? public void doAfterReturning(JoinPoint jp, Object object) throws Exception {
? ? ? ? applicationContext.publishEvent(new LogSuccessEvent(jp, null));

? ? }

? ? //异常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法异常,异常信息:" + ex.getMessage());
? ? ? ? applicationContext.publishEvent(new LogSuccessEvent(jp, ex));
? ? }

? ? /**
? ? ?* 是否存在注解,如果存在就获取
? ? ?*/
? ? private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
? ? ? ? Signature signature = joinPoint.getSignature();
? ? ? ? MethodSignature methodSignature = (MethodSignature) signature;
? ? ? ? Method method = methodSignature.getMethod();
? ? ? ? if (method != null) {
? ? ? ? ? ? return method.getAnnotation(Log.class);
? ? ? ? }
? ? ? ? return null;
? ? }
}
@Slf4j
@Service
public class ApplicationListener implements org.springframework.context.ApplicationListener<LogSuccessEvent> {

? ? @Autowired
? ? private ServiceDemo demo;

? ? @Override
? ? public void onApplicationEvent(LogSuccessEvent event) {
? ? ? ? JoinPoint joinPoint = event.getJp();
? ? ? ? Throwable ex = event.getThrowable();
? ? ? ? if (ex == null) {
? ? ? ? ? ? demo.demo();
? ? ? ? } else {
? ? ? ? ? ? demo.error();
? ? ? ? }
? ? }

}
@Slf4j
public class LogSuccessEvent extends ApplicationEvent {
? ? /**
? ? ?* Create a new ApplicationEvent.
? ? ?*
? ? ?* @param source the component that published the event (never {@code null})
? ? ?*/
? ? private JoinPoint jp;

? ? private Throwable throwable;

? ? public LogSuccessEvent(Object source, Throwable throwable) {
? ? ? ? super(source);
? ? ? ? this.throwable = throwable;
? ? ? ? this.jp = (JoinPoint) source;
// ? ? ? ?Log logger = (Log) source;
// ? ? ? ?log.info(logger.title());
// ? ? ? ?log.info(logger.business());
// ? ? ? ?log.info(logger.user());
// ? ? ? log.info(logger.isSaveRequestData() + "");

? ? }

? ? public JoinPoint getJp() {
? ? ? ? return jp;
? ? }

? ? public Throwable getThrowable() {
? ? ? ? return throwable;
? ? }
}

来源:https://blog.csdn.net/weixin_43864445/article/details/105409686

标签:springboot,aop,切面日志
0
投稿

猜你喜欢

  • C#基于正则表达式删除字符串中数字或非数字的方法

    2021-07-12 17:27:44
  • Java并发编程之详解CyclicBarrier线程同步

    2023-08-01 14:08:47
  • Android Handler主线程和一般线程通信的应用分析

    2022-10-29 14:08:13
  • Java中的zookeeper常用命令详解

    2022-12-07 05:46:35
  • C#模拟实现抽奖小程序的示例代码

    2021-08-27 22:19:17
  • 简单了解JAVA public class与class区别

    2023-11-15 23:59:26
  • Android入门之Handler的使用教程详解

    2022-06-17 04:03:22
  • SpringMVC静态资源访问问题如何解决

    2021-06-04 15:58:55
  • 详解SpringBoot注册Windows服务和启动报错的原因

    2022-12-28 17:10:09
  • Java注解方式之防止重复请求

    2023-05-29 16:30:51
  • Android仿微信朋友圈图片查看器

    2023-01-27 18:07:20
  • Java集合系列之ArrayList源码分析

    2023-01-31 03:02:36
  • Android编程实现Listview点击展开和隐藏的方法

    2023-04-26 01:36:49
  • Springboot如何根据实体类生成数据库表

    2023-11-20 13:54:39
  • C#在DataTable中根据条件删除某一行的实现方法

    2022-03-09 05:34:19
  • 基于Android LayoutInflater的使用介绍

    2023-12-22 23:50:03
  • 详解Java实现多线程的三种方式

    2021-10-30 03:19:16
  • mybatis-plus3.0.1枚举返回为null解决办法

    2023-11-07 16:59:50
  • Java开发人员最常犯的10个错误

    2021-10-25 16:40:08
  • Java中类的定义与实例化详解

    2022-10-25 23:18:43
  • asp之家 软件编程 m.aspxhome.com