如何使用 Spring AOP(AspectJ 风格)访问方法属性?
我需要通过使用注释作为切入点来截取一些方法及其属性,但是如何访问这些方法属性。我有以下代码,可以在方法运行之前成功运行代码,但我只是不知道如何访问这些属性。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从
ProceedingJoinPoint
对象获取它们:You can get them from the
ProceedingJoinPoint
object:ProceedingJoinPoint
有pjp.getArgs()
,它返回该方法的所有参数。(但这些被称为参数/参数,而不是属性)
ProceedingJoinPoint
haspjp.getArgs()
, which returns all the parameters of the method.(but these are called parameters / arguments, not attributes)