zoukankan      html  css  js  c++  java
  • spring-AOP-添加日志

    1 把一个类声明为一个切面:①需要把该类放入到IOC中,②再声明为一个切面(@Aspect @Component)@Order(1):指定顺序

    2 在配置文件中添加如下配置:<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    package com.atguigu.aop;
    
    import java.util.Arrays;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    /*
     * 把一个类声明为一个切面:1,需要把该类放入到IOC容器中;2,再声明为切面
     * 
     */
    @Aspect
    @Component
    public class LoggingAspect {
    
        /*定义一个方法,用于声明切入点表达式,一般地,该方法不需要添加其他的代码
         * 使用@PointCut来声明切入点表达式
         * 后面的其他通知直接使用方法名来引用当前的切入点表达式
         * 
         */
        @Pointcut("execution(* com.atguigu.aop.*.*(..))")
        public void declareJoinPointExpression(){}
        
        @Before("declareJoinPointExpression()")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            Object [] args = joinPoint.getArgs();
            
            System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
        }
        //无论是否有异常都会执行
        @After("declareJoinPointExpression()")
        public void after(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("The method " + methodName + " end " );
        }
        
        @AfterReturning(value="declareJoinPointExpression()",returning="result")
        public void afterReturning(JoinPoint joinPoint,Object result){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("The method " + methodName + " ends with " + result);
        }
        
        @AfterThrowing(value="declareJoinPointExpression()",throwing="ex")
        public void afterThrowing(JoinPoint joinPoint,Exception ex){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("The method " + methodName + " occurs with " + ex);
        }
        /*
         *     环绕通知需要传递一个ProceedingJoinPoint参数
         *     环绕通知类似于动态代理的全过程;ProceedingJoinPoint类型的参数可以决定是否执行目标方法
         *     环绕通知必须要有返回值,该返回值即使目标方法的返回值
         */
    //    @Around("execution(* com.atguigu.aop.*.*(..))")
    //    public Object around(ProceedingJoinPoint point){
    //        Object result = null;
    //        String methodName = point.getSignature().getName();
    //        try {
    //            //前置通知
    //            System.out.println("The method "+methodName+" start with "+Arrays.asList(point.getArgs()));
    //            result = point.proceed();
    //            //返回通知
    //            System.out.println("The method "+methodName+" end with "+result);
    //        } catch (Throwable e) {
    //            //异常通知
    //            System.out.println("The method "+methodName+" occurs with "+e);
    //            e.printStackTrace();
    //        }
    //        //后置通知
    //        System.out.println("The method "+methodName+" end");
    //        return result;
    //    }
    }
  • 相关阅读:
    升级windows 11小工具
    windows 10更新升级方法
    您需要了解的有关 Oracle 数据库修补的所有信息
    Step by Step Apply Rolling PSU Patch In Oracle Database 12c RAC Environment
    Upgrade Oracle Database Manually from 12.2.0.1 to 19c
    如何应用版本更新 12.2.0.1.210420(补丁 32507738 – 2021 年 4 月 RU)
    xtrabackup 安装、备份和恢复
    Centos_Lvm expand capacity without restarting CentOS
    Centos_Lvm_Create pv vg lv and mount
    通过全备+relaylog同步恢复被drop的库或表
  • 原文地址:https://www.cnblogs.com/james-roger/p/5032437.html
Copyright © 2011-2022 走看看