java joinpoint教程_aop 中joinpoint的使用方法

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

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

publicvoidaround(ProceedingJoinPoint joinpoint) {

joinpoint.proceed();

}public void around(ProceedingJoinPoint joinpoint) {

joinpoint.proceed();

}

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

publicvoidbefore(JoinPoint joinpoint) {

}public void before(JoinPoint joinpoint) {

}

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

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

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

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

joinpoint.getSignature().getDeclaringTypeName();//接口全路徑

joinpoint.getSignature().getName();//調用的方法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());

}

}

Java AOP ,无法直接通过 `JoinPoint` 获取 POST 参数。`JoinPoint` 接口提供了一组方法来获取方法的相关信息,如方法名、参数等,但它不直接提供获取 HTTP 请求参数的功能。 要获取 POST 参数,你可以使用 Spring AOP 结合 Spring MVC 的功能来实现。在 Spring MVC ,可以使用 `HttpServletRequest` 对象来获取请求参数。通过将 `HttpServletRequest` 作为切面方法的参数,在切面方法可以通过它来获取 POST 参数。下面是一个简单的示例: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Aspect @Component public class MyAspect { @Before("execution(* com.example.MyController.myMethod(..)) && args(.., request)") public void beforeMethod(JoinPoint joinPoint, HttpServletRequest request) { // 获取 POST 参数 String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); // 处理参数 // ... } } ``` 上述代码,`MyController` 是你的控制器类,`myMethod` 是你想要拦截的方法。在切面方法 `beforeMethod` ,通过将 `HttpServletRequest` 作为参数,并使用 `request.getParameter` 方法来获取 POST 参数。 需要注意的是,这种方式仅适用于 Spring MVC,并且需要确保你的项目已经配置了正确的请求处理器和拦截器。另外,如果你使用的是其他 Java Web 框架,可以根据框架提供的方式获取 POST 参数。 希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值