Dear all I am curently using Spring AOP (v4) and AspectJ with load-time-weaver.
I am looking currently for a way to add a dirty flag mechanism into my beans. Therefore I I though of using AOP to call a method before a setter of my beans get called. This I achieved already, but how can I access the old field value beforeit get modified? Or is there a way to get the field name so I can call the getter before the setter get called?
Can anybody provide me here some example how the pointcut/advice has to look like to get it a passed as arguments?
@Aspect
public class MyAspect {
@Before("execution(* foo.*.set*(..))")
public void beforeSetterCalled(JoinPoint joinPoint){
System.out.println("beforeSetter");
}
}
Unfortunately it seems that Spring AOP does not support the "set()" field-pointcut construct, is this correct? OR exists someway to use this?
Thank you for any help.
You are correct about Spring not supporting field joinpoints
You won't be able to use Spring AOP to get the field value through the AOP advice directly.
A method is not related to any field. Accessors (and mutators) are just a convention in Java. If you are following that convention, you can infer the field name from the method name and use reflection to retrieve it.
I would recommend to use full AspectJ in combination with a
set()
pointcut in order to get an efficient solution. But if you do not mind having a slow, ugly solution involving reflection you can also do something like this:Console log: