SpringBoot中aop大概用法

1.pom.xml中引入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.wood.logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 *切面
 */
@Aspect
@Component
public class AspectTest {
    @Pointcut("execution(* com.wood.controller.fmodule.F02Controller.complete(..))")
    public void webLog(){}
    
    @Before("webLog()")
    public void before(JoinPoint jp){
        System.out.println(jp.getSignature().getName()+"方法开始执行了==============================");
    }
    @After("webLog()")
    public void after(JoinPoint jp){
        System.out.println(jp.getSignature().getName()+"方法开始执行结束了==============================");
    }
   //声明环绕通知
    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startDate = System.currentTimeMillis();
        Object o = pjp.proceed();
        long endDate = System.currentTimeMillis();
        System.out.println(pjp.getSignature().getName()+"方法用时毫秒数为:"+(endDate -startDate )+"===========================================================");
        return o;
    }
}

参数说明:
@Aspect:注解将一个java类定义为切面类
@Pointcut:定义一个切点
@Before:在切点开始处切入内容
@After:在切点结尾处切入内容
@AfterReturning:在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
@Around:在切入点前后切入内容,并自己控制何时执行切入点自身的内容
@AfterThrowing:用来处理当切入内容部分抛出异常之后的处理逻辑

execution(* com.wood.com.wood.controller.fmodule.F02Controller.complete(…)))第一个“*”号表示方法的返回值,com.wood.com.wood.controller.fmodule.F02Controller.complete表示找到此方法。(. .)表示参数

JoinPoint 里面可以查看被哪些方法调用了

猜你喜欢

转载自blog.csdn.net/qq_41373328/article/details/88752190
今日推荐