• 多个切面执行同一个方法


    1.建立日志切面

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    
    //开启AspectJ 自动代理模式,如果不填proxyTargetClass=true,默认为false,
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @Component
    @Aspect
    @Order(1)
    public class ControllerLogAspectConfig {
        @Around("execution(* com.g2.order.server.controller.*.*(..))")
        public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            System.out.println("进入日志切面");
    
            //获取controller对应的方法.
            org.aspectj.lang.reflect.MethodSignature methodSignature = (org.aspectj.lang.reflect.MethodSignature) proceedingJoinPoint.getSignature();
            //获取方法所在的类(controller)
            Class beanType = methodSignature.getDeclaringType();
            //获取方法
            Method method = methodSignature.getMethod();
    
            //获取方法参数列表(无需处理讨厌的流了)
            Object[] args = proceedingJoinPoint.getArgs();
            for (Object arg : args) {
                //获取参数的类型与值
                System.out.println(arg.getClass().getName());
                System.out.println("arg is " + arg);
            }
    
    
            System.out.println("进入其他切面..");
            Object obj = proceedingJoinPoint.proceed();
            //获取返回值的类型,与 Method.getReturnType()一致
            Class responseClass=obj.getClass();
    
            //方法的返回值是:
            System.out.println("response is " + obj);
            System.out.println("业务完成,日志已记录");
            return obj;
        }
    }

    2.建立执行时间切面 

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.After;
    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.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    
    import javax.annotation.Resource;
    
    //开启AspectJ 自动代理模式,如果不填proxyTargetClass=true,默认为false,
    @Component
    @Aspect
    @Order(Ordered.LOWEST_PRECEDENCE)
    public class ControllerTimeAspectConfig {
        @Around("execution(* com.g2.order.server.controller.*.*(..))")
        public Object handleControllerMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            System.out.println("进入时间切面,执行before..");
    
    
            System.out.println("进入业务执行..");
            long startTime = System.currentTimeMillis();
    
            Object obj = proceedingJoinPoint.proceed();
    
            System.out.println("time aspect 耗时" + (System.currentTimeMillis() - startTime));
    
    
            System.out.println("业务完成,执行时间已记录");
            return obj;
        }
    }

    3.执行结果

    2018-09-20 15:19:18.908  INFO 14688 --- [p-nio-88-exec-1] c.g.o.s.i.AccessLogInterceptor           : 请求方法:login,请求参数类型:com.g2.order.server.vo.user.UserLoginReq,请求值:{    "userId":"123","password":"123444"}
    进入日志切面
    com.g2.order.server.vo.user.UserLoginReq
    arg is UserLoginReq{userId='123', password='123444'}
    进入其他切面..
    进入时间切面,执行before..
    进入业务执行..
    2018-09-20 15:19:22.120  INFO 14688 --- [p-nio-88-exec-1] c.g.o.server.controller.HomeController   : 进入登陆业务
    2018-09-20 15:19:22.176  INFO 14688 --- [p-nio-88-exec-1] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
    time aspect 耗时841
    业务完成,执行时间已记录
    response is Response{success=true, errorMessage='', payload=UserModel{userId='1', roleName='null', roleId=null}}
    业务完成,日志已记录
  • 相关阅读:
    组合与计数
    20160929训练记录
    奇特而有用的定理
    图论 500 题
    《长安十二时辰》愿你看尽世间百态,心中仍有热血
    洛谷 [P1337] 平衡点
    洛谷 [P3496] BLO
    洛谷 [P2341] 受欢迎的牛
    洛谷 [P3723] 礼物
    洛谷 [P3338] 力
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/9681262.html
Copyright © 2020-2023  润新知