• AOP的应用(日志打印)


    package com.ly.mp.iov.config;

    import com.alibaba.fastjson.JSON;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;

    @Component
    @Aspect
    public class LoggingAspect {

    private static Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
    
    /*
     *定义一个方法,用于声明切点表达式,该方法一般没有方法体
     *@Pointcut用来声明切点表达式
     *通知直接使用定义的方法名即可引入当前的切点表达式
     */
    @Pointcut("execution(* com.ly.mp.iov.service.*.*(..))")
    public void pointcutDeclaration() {}
    
    //前置通知,方法执行之前执行
    @Before("pointcutDeclaration()")
    public void beforeMethod(JoinPoint jp) {
        String targetClassName = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        String param = null;
        if( jp.getArgs() != null  ) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append( "[" );
            for( Object arg : jp.getArgs() )
            {
                if( stringBuffer.toString().length() > 1 ) {
                    stringBuffer.append( "," );
                }
                try {
                    stringBuffer.append( JSON.toJSONString( arg ) );
                }catch (Exception e) {
                    stringBuffer.append( "unSupportType" );
                }
    
            }
            stringBuffer.append( "]" );
            param = stringBuffer.toString();
        }
        logger.info( "{}.{} receive data:{}" , targetClassName , methodName , param );
    }
    
    //后置通知,方法执行之后执行(不管是否发生异常)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    // @After(“pointcutDeclaration()”)
    // public void AfterMethod(JoinPoint jp) {
    // String methodName = jp.getSignature().getName();
    // Object[] args = jp.getArgs();
    // System.out.println("AfterMethod The method “+ methodName +” parameter is "+Arrays.asList(args));
    // System.out.println();
    // }

    //返回通知,方法正常执行完毕之后执行
    @AfterReturning(value="pointcutDeclaration()",returning="result")
    public void afterReturningMethod(JoinPoint jp , Object result) {
        String targetClassName = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        logger.info( "{}.{} return data:{}" , targetClassName , methodName , JSON.toJSONString( result ) );
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    }

  • 相关阅读:
    大咖说*计算讲谈社|AI 的价值探索:如何拓展商业边界?
    【边缘检测】基于蚁群算法实现图像边缘检测附matlab代码
    运动想象 (MI) 迁移学习系列 (9) : 数据对齐(EA)
    5.36 BCC工具之ucalls.py解读
    Windows 10驱动开发入门(四):USB下的过滤器驱动
    python标准库之itertools
    DAY01------TCP/IP协议、子网掩码、默认网关、查看IP地址参数 、测试网络连通性
    vue3 - Vue 项目处理GitHub Pages 部署后 _plugin-vue_export-helper.js 404
    EF7创建模型继承映射篇
    2024.4.24
  • 原文地址:https://blog.csdn.net/Fzicoo/article/details/126036913