Springboot基于aop自定义注解(方法耗时)

使用效果

在方法上使用自定义注解@RunTime

在这里插入图片描述

调用接口后

在这里插入图片描述

1、aop依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>2.5.2</version>
</dependency>

2、新建aop类

在类上使用@Aspect、@Component注解

@Aspect
@Component//使用spring容器进行管理
@Slf4j
public class RunTimeAspect {
	
}

3、使用@Pointcut定义一个切点

@Pointcut表达式


    execution():用于匹配方法执行的连接点
    args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法
    this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
    target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
    within(): 用于匹配指定类型内的方法执行;
    @args():于匹配当前执行的方法传入的参数持有指定注解的执行;
    @target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
    @within():用于匹配所以持有指定注解类型内的方法;
    @annotation:用于匹配当前执行方法持有指定注解的方法;

其中execution 是用的最多的,

代码示例

@Aspect
@Component//使用spring容器进行管理
@Slf4j
public class RunTimeAspect {
	//定义一个切点。指向我们的注解类,注意路径要与Annotation类一致
	@Pointcut(value = "@annotation(com.example.myAnnotation.RunTime)")
	public void RunTime(){
	}
}

4、使用@Around环绕通知,输出时间

// 使用@Around环绕通知, 环绕通知=前置通知+目标方法执行+后置通知
	@Around("RunTime()")
	public Object  doAround(ProceedingJoinPoint joinPoint) { //ProceedingJoinPoint可以获取当前方法和参数等信息
		Object obj = null;
		try {
			long beginTime = System.currentTimeMillis();
			obj = joinPoint.proceed();
			//获取方法名称
			String methodName = joinPoint.getSignature().getName();
			//获取类名称
			String className = joinPoint.getSignature().getDeclaringTypeName();
			log.info("类:[{}],方法:[{}]耗时时间为:[{}]", className, methodName, System.currentTimeMillis() - beginTime + "毫秒");
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return obj;
	}

5、其他常用注解

方法作用
@Before前置通知、在切点方法之前执行
@After后置通知、在切点方法之后执行
@AfterReturning后置通知、切点方法返回后执行
@AfterThrowing切点方法抛异常执行
@Around环绕通知、执行前和执行后通知,近似于Before增强处理和AfterReturing增强处理的总结

6、新建Annotation类(用于注解)

在这里插入图片描述

7、代码示例

@Documented  //用于标明该注解将会被包含在javadoc中
@Retention(RetentionPolicy.RUNTIME) //用于标明注解的生命周期
@Target({ElementType.METHOD}) //用于标明注解的作用范围
public @interface RunTime {
}

8、全部代码

1、RunTime 类

package com.example.myAnnotation;

import java.lang.annotation.*;

@Documented  //用于标明该注解将会被包含在javadoc中
@Retention(RetentionPolicy.RUNTIME) //用于标明注解的生命周期
@Target({ElementType.METHOD}) //用于标明注解的作用范围
public @interface RunTime {
}

2、aop类

package com.example.myAnnotation;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component//使用spring容器进行管理
@Slf4j
public class RunTimeAspect {
	//定义一个切点。指向我们的注解类
	@Pointcut(value = "@annotation(com.example.myAnnotation.RunTime)")
	public void RunTime(){
	}
// 使用@Around环绕通知, 环绕通知=前置通知+目标方法执行+后置通知
	@Around("RunTime()")
	public Object  doAround(ProceedingJoinPoint joinPoint) { //ProceedingJoinPoint可以获取当前方法和参数等信息
		Object obj = null;
		try {
			long beginTime = System.currentTimeMillis();
			obj = joinPoint.proceed();
			//获取方法名称
			String methodName = joinPoint.getSignature().getName();
			//获取类名称
			String className = joinPoint.getSignature().getDeclaringTypeName();
			log.info("类:[{}],方法:[{}]耗时时间为:[{}]", className, methodName, System.currentTimeMillis() - beginTime + "毫秒");
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return obj;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值