// 引入必要的依赖
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
// 定义一个切点,匹配所有以 "execute" 开头的方法
@Pointcut("execution(* com.example.service..*execute*(..))")
public void executeMethods() {}
// 在匹配的方法执行之前执行的逻辑
@Before("executeMethods()")
public void logBefore() {
System.out.println("Method is about to execute.");
}
// 在匹配的方法执行之后执行的逻辑
@After("executeMethods()")
public void logAfter() {
System.out.println("Method has been executed.");
}
}
com.example.service 包下所有以 execute 开头的方法。通过这种方式,你可以在不修改业务代码的情况下,增强方法的行为,例如添加日志记录、性能监控等功能。
上一篇:java 打印数组
下一篇:java解析json
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站