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;
}
}
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
my abc class is as follow
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?
@Hemant
You can access the declaring type and its fields from the ProceedingJointPoint object as follow:
Field & Method are part of the java.lang.reflect package
If you are using Spring then you could use the AOPUtils helper class
You need to get a reference to the target object and cast it to your class (after an
instanceof
check, perhaps):The recommended approach (for performance and type safety) is to have the target mentioned in the pointcut:
But this will only match the executions on a target of type Abc.