访问方面类中的类变量 [英] access class variable in aspect class

查看:106
本文介绍了访问方面类中的类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring aspectj创建一个方面类,如下所示

i am creating an aspect class with spring aspectj as follow

@Aspect
public class AspectDemo {
  @Pointcut("execution(* abc.execute(..))")
     public void executeMethods() { }

 @Around("executeMethods()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("Going to call the method.");
            Object output = pjp.proceed();
            System.out.println("Method execution completed.");
            return output;
    }

} 

现在我要访问该物业类abc的名称然后如何在方面类中访问它?
i想要在配置文件方法中显示abc类的名称属性

now i want to access the property name of class abc then how to acccess it in aspect class? i want to display name property of abc class in profile method

我的abc类如下

public class abc{
String name;

public void setName(String n){
name=n;
}
public String getName(){
 return name;
}

public void execute(){
System.out.println("i am executing");
}
}

如何在方面类中访问该名称?

How can i access the name in aspect class?

推荐答案

您需要获取对目标对象的引用并将其强制转换为您的类(在 instanceof之后) 检查,或许):

You need to get a reference to the target object and cast it to your class (after an instanceof check, perhaps):

Object target = pjp.getTarget();
if (target instanceof Abc) {
    String name = ((Abc) target).getName();
    // ...
}

建议的方法(用于表现和类型安全)是指切入点中提到的目标:

The recommended approach (for performance and type safety) is to have the target mentioned in the pointcut:

@Around("executeMethods() && target(abc)")
public Object profile(ProceedingJoinPoint pjp, Abc abc) ....

但这只会匹配Abc类型目标的执行情况。

But this will only match the executions on a target of type Abc.

这篇关于访问方面类中的类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆