多個切面執行同一個方法


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}}
業務完成,日志已記錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM