如何使用 Spring AOP(AspectJ 风格)访问方法属性?

发布于 2024-09-29 11:10:22 字数 708 浏览 0 评论 0原文

我需要通过使用注释作为切入点来截取一些方法及其属性,但是如何访问这些方法属性。我有以下代码,可以在方法运行之前成功运行代码,但我只是不知道如何访问这些属性。

package my.package;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

 @Pointcut(value="execution(public * *(..))")
 public void anyPublicMethod() {
 }

 @Around("anyPublicMethod() && @annotation(myAnnotation )")
 public Object myAspect(ProceedingJoinPoint pjp, MyAnnotation myAnnotation)
    throws Throwable {

  // how can I access method attributes here ?
  System.out.println("hello aspect!");
  return pjp.proceed();
 }
}

I need to intrecept some methods and their attributes by using annotations as point cuts, but how can I access those method attributes. I have following code that succesfully can run code before method is run, but I just don't know how I can access those attrbiutes.

package my.package;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

 @Pointcut(value="execution(public * *(..))")
 public void anyPublicMethod() {
 }

 @Around("anyPublicMethod() && @annotation(myAnnotation )")
 public Object myAspect(ProceedingJoinPoint pjp, MyAnnotation myAnnotation)
    throws Throwable {

  // how can I access method attributes here ?
  System.out.println("hello aspect!");
  return pjp.proceed();
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-10-06 11:10:22

您可以从 ProceedingJoinPoint 对象获取它们:

@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(final ProceedingJoinPoint pjp,
    final MyAnnotation myAnnotation) throws Throwable{

    // retrieve the methods parameter types (static):
    final Signature signature = pjp.getStaticPart().getSignature();
    if(signature instanceof MethodSignature){
        final MethodSignature ms = (MethodSignature) signature;
        final Class<?>[] parameterTypes = ms.getParameterTypes();
        for(final Class<?> pt : parameterTypes){
            System.out.println("Parameter type:" + pt);
        }
    }

    // retrieve the runtime method arguments (dynamic)
    for(final Object argument : pjp.getArgs()){
        System.out.println("Parameter value:" + argument);
    }

    return pjp.proceed();
}

You can get them from the ProceedingJoinPoint object:

@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(final ProceedingJoinPoint pjp,
    final MyAnnotation myAnnotation) throws Throwable{

    // retrieve the methods parameter types (static):
    final Signature signature = pjp.getStaticPart().getSignature();
    if(signature instanceof MethodSignature){
        final MethodSignature ms = (MethodSignature) signature;
        final Class<?>[] parameterTypes = ms.getParameterTypes();
        for(final Class<?> pt : parameterTypes){
            System.out.println("Parameter type:" + pt);
        }
    }

    // retrieve the runtime method arguments (dynamic)
    for(final Object argument : pjp.getArgs()){
        System.out.println("Parameter value:" + argument);
    }

    return pjp.proceed();
}
烂人 2024-10-06 11:10:22

ProceedingJoinPointpjp.getArgs(),它返回该方法的所有参数。

(但这些被称为参数/参数,而不是属性)

ProceedingJoinPoint has pjp.getArgs(), which returns all the parameters of the method.

(but these are called parameters / arguments, not attributes)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文