aop 中joinpoint的使用方法

本文详细解析了AOP中的JoinPoint概念及其应用场景,包括如何使用JoinPoint控制代码执行流程和获取方法调用的元信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.簡述下joinpoint在不同情況下的不同:

 

1.在around中可以用,此時可以執行被包裹的代碼,可以根據情況來判斷是否執行被包裹的代碼,以實現控制的作用。

public void around(ProceedingJoinPoint joinpoint) {
	joinpoint.proceed();
}

2.別的用,常用於做記錄之用,不能控制被包裹的代碼是否執行。

public void before(JoinPoint joinpoint) {

}

主要區別:在於是否能控制被包裹的代碼。


二.joinpoint如何查看類調用情況:

joinpoint.getArgs();//輸入的參數列表

joinpoint.getTarget().getClass().getName();//類全路徑

joinpoint.getSignature().getDeclaringTypeName();//接口全路徑
		
joinpoint.getSignature().getName();//調用的方法


三.什麼是Signature

public interface Signature
 

Represents the signature at a join point. This interface parallels java.lang.reflect.Member.

This interface is typically used for tracing or logging applications to obtain reflective information about the join point, i.e. using the j2se 1.4java.util.logging API

 aspect Logging {
     Logger logger = Logger.getLogger("MethodEntries");
 
     before(): within(com.bigboxco..*) && execution(public * *(..)) {
         Signature sig = thisJoinPoint.getSignature();
         logger.entering(sig.getDeclaringType().getName(),
                         sig.getName());
     }
 }
 

 

api網址:http://www.eclipse.org/aspectj/doc/next/runtime-api/

### Java AOPJoinPoint使用方法及示例 #### 什么是 JoinPoint? 在面向切面编程(AOP)中,`JoinPoint` 是指程序运行过程中可以被拦截的具体点。它表示应用执行流程中的某个时刻,比如方法的调用或异常抛出的位置[^4]。 #### `JoinPoint` 接口的功能 `JoinPoint` 提供了一些有用的方法来获取当前连接点的信息,例如: - **`getArgs()`**: 返回方法参数数组。 - **`getSignature()`**: 获取正在被执行的方法签名。 - **`getTarget()`**: 返回目标对象(即实际被代理的对象)。 - **`getThis()`**: 返回代理对象本身。 对于环绕通知 (`@Around`),还可以通过其子接口 `ProceedingJoinPoint` 调用 `proceed()` 方法显式地控制目标方法的执行。 --- #### 示例代码展示 下面是一个简单的例子,展示了如何在 Spring AOP使用 `JoinPoint`: ```java package com.example.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Component @Aspect public class LoggingAspect { // 定义一个前置通知,在指定方法执行前打印日志 @Before("execution(* com.example.service.*.*(..))") public void logMethodCall(JoinPoint joinPoint) { StringBuilder sb = new StringBuilder(); // 获取方法名 sb.append("Method called: ").append(joinPoint.getSignature().getName()).append("\n"); // 获取方法参数 Object[] args = joinPoint.getArgs(); if (args.length > 0) { sb.append("Arguments passed to method:"); for (Object arg : args) { sb.append(" ").append(arg); } sb.append("\n"); } System.out.println(sb.toString()); } } ``` 在这个例子中,我们定义了一个名为 `LoggingAspect` 的切面类,并在其内部创建了一个前置通知 (`@Before`)。该通知会在任何匹配表达式的业务方法执行之前触发,并利用 `JoinPoint` 来记录方法名称及其参数信息[^3]。 如果需要更复杂的逻辑处理,则可以通过引入 `ProceedingJoinPoint` 实现环绕通知: ```java @Around("execution(* com.example.service.*.*(..))") public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); try { // 执行原始方法并捕获返回值 Object result = pjp.proceed(); // 记录耗时 long elapsedTime = System.currentTimeMillis() - start; System.out.println("Execution time of " + pjp.getSignature().getName() + ": " + elapsedTime + "ms"); return result; } catch (Throwable e) { throw e; } } ``` 上述代码片段实现了性能监控功能——计算每次服务层方法调用所花费的时间。 --- ### 总结 通过对 `JoinPoint` 和它的扩展版本 `ProceedingJoinPoint` 的学习与实践,开发者能够更加灵活地操控横切关注点的行为模式,从而提升系统的模块化程度和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值