Java Reference
In-Depth Information
pojo.setOne("one");
pojo.setTwo(22);
pojo.setThree(333.0);
assertEquals("one", pojo.getOne());
assertEquals(22, pojo.getTwo());
assertEquals(333.0, pojo.getThree(),0.0001);
}
}
Spring injects an instance of the POJO into the test and executes the test, which simply
calls the three setters and checks that they work properly. The interesting part is in the con-
sole output, which shows the aspect in play:
INFO: setOne about to change to one on POJO [one=1, two=2, three=3.0]
INFO: setTwo about to change to 22 on POJO [one=one, two=2, three=3.0]
INFO: setThree about to change to 333.0 on POJO [one=one, two=22, three=3.0]
The aspect reports the name of each set method and its argument when it's called.
Everything works as advertised.
There's one issue, though. What if you want to know the current value of each property
before the setter changes it? There's no obvious way to find out. The joinpoint gives access
to the target, and I know that a set method is being called, but while I know conceptually
that for every setter there's a getter, figuring out how to invoke it isn't trivial. Determining
the proper get method could probably be done with a combination of reflection and string
manipulation, but there's work involved.
At least, there's work involved unless I appeal to Groovy. I can do everything I just de-
scribed in a handful of lines of Groovy, as the next listing demonstrates.
Listing 7.18. A Groovy aspect for printing property values before they are changed
package mjg.aspects
import java.util.logging.Logger
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
@Aspect
class UpdateReporter {
Logger log = Logger.getLogger(UpdateReporter.class.name)
Search WWH ::




Custom Search