当前位置: 首页 > news >正文

AspectJ 在项目中的集成应用代码案例

1. 添加 AspectJ 依赖

在 Maven 项目的 pom.xml 文件中添加以下依赖:

<dependencies><!-- AspectJ Weaver --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.6</version></dependency><!-- AspectJ 运行时 --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.6</version></dependency>
</dependencies>

2. 创建切面类

创建一个切面类,用于定义日志记录和性能监控的逻辑。

日志记录切面
package com.example.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LoggingAspect {// 定义切点,匹配 com.example.service 包下所有类的所有方法@Pointcut("execution(* com.example.service.*.*(..))")public void serviceMethods() {}// 前置通知@Before("serviceMethods()")public void logBefore(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();String className = joinPoint.getTarget().getClass().getName();System.out.println("Before: Calling method " + methodName + " in class " + className);}// 后置通知@After("serviceMethods()")public void logAfter(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();System.out.println("After: Method " + methodName + " executed");}// 返回通知@AfterReturning(pointcut = "serviceMethods()", returning = "result")public void logAfterReturning(JoinPoint joinPoint, Object result) {String methodName = joinPoint.getSignature().getName();System.out.println("AfterReturning: Method " + methodName + " returned with result: " + result);}// 异常通知@AfterThrowing(pointcut = "serviceMethods()", throwing = "ex")public void logAfterThrowing(JoinPoint joinPoint, Throwable ex) {String methodName = joinPoint.getSignature().getName();System.out.println("AfterThrowing: Method " + methodName + " threw exception: " + ex.getMessage());}
}
性能监控切面
package com.example.aspect;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;import java.util.logging.Logger;@Aspect
@Component
public class PerformanceMonitorAspect {private static final Logger logger = Logger.getLogger(PerformanceMonitorAspect.class.getName());// 定义切点,匹配 com.example.service 包下所有类的所有方法@Pointcut("execution(* com.example.service.*.*(..))")public void serviceMethods() {}// 环绕通知@Around("serviceMethods()")public Object profile(ProceedingJoinPoint pjp) throws Throwable {long startTime = System.currentTimeMillis();Object result = pjp.proceed(); // 调用目标方法long elapsedTime = System.currentTimeMillis() - startTime;logger.info("Method execution time: " + elapsedTime + " ms for method: " + pjp.getSignature().toShortString());return result;}
}

3. 创建服务类

创建一个简单的服务类,用于测试切面功能。

package com.example.service;import org.springframework.stereotype.Service;@Service
public class MyService {public String doSomething() {System.out.println("Executing doSomething method");return "Result";}
}

4. 测试

在主程序或测试类中调用服务类的方法,验证切面是否生效。

package com.example;import com.example.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example");MyService myService = context.getBean(MyService.class);myService.doSomething();context.close();}
}

输出示例

运行程序后,控制台输出类似以下内容:

Before: Calling method doSomething in class com.example.service.MyService
Executing doSomething method
AfterReturning: Method doSomething returned with result: Result
After: Method doSomething executed
Method execution time: 10 ms for method: doSomething()

说明

  • 日志记录切面:通过 @Before@After@AfterReturning@AfterThrowing 注解,分别在方法执行前后、返回后和抛出异常后记录日志。

  • 性能监控切面:通过 @Around 注解,记录方法的执行时间。

  • 切点表达式execution(* com.example.service.*.*(..)) 表示匹配 com.example.service 包下所有类的所有方法。

通过以上代码,你可以轻松地在 Spring 项目中集成 AspectJ,实现日志记录、性能监控等功能,而无需修改业务逻辑代码

相关文章:

  • VR教育:开启教育新时代的钥匙
  • Rhino插件大全下载指南:解锁犀牛潜能,提升设计效率
  • 基于大模型的慢性硬脑膜下血肿预测与诊疗系统技术方案
  • LabVIEW基于 DataSocket从 OPC 服务器读取数据
  • 【机器学习及深度学习】机器学习模型的误差:偏差、方差及噪声
  • HDFS 写入和读取流程
  • 40、响应处理-【源码分析】-基于请求参数的内容协商原理
  • Flink 失败重试策略 :restart-strategy.type
  • 学习threejs,交互式神经网络可视化
  • 三、kafka消费的全流程
  • 论文分类打榜赛Baseline:ms-swift微调InternLM实践
  • LangChain基本概念
  • Numpy入门2——视图和副本、伪随机数、切片和索引、数组的轴操作
  • Python训练打卡Day41
  • BugKu Web渗透之game1
  • 20250603在荣品的PRO-RK3566开发板的Android13下的使用命令行来查看RK3566的温度【显示优化版本】
  • 【 java 集合知识 第一篇 】
  • Python趣学篇:从零打造智能AI井字棋游戏(Python + Tkinter + Minimax算法)
  • 云原生周刊:探索 Gateway API v1.3.0
  • 深入理解Android进程间通信机制
  • php网站开发程序员/外国黄冈网站推广平台
  • 东莞最新疫情地区/聊城seo
  • css 10个网站/电子商务推广
  • 秦皇岛做网站公司有哪些/51link友链
  • 计算机网站建设的能力/青岛seo霸屏
  • 珠宝网站源码免费下载/竞价托管公司排名