Java Reference
In-Depth Information
The
@Aspect
annotation tells Spring this is an aspect. The
@Before
annotation defines
methods that begin with the letters
set
that take a single argument and return
void
.
The
trackChange
method is the advice. The
JoinPoint
argument is supplied by
Spring when the aspect is called. It provides context for the execution. In this case, the
JoinPoint
has methods to retrieve the signature of the method being advised, as well as
the arguments supplied to the method and the target object.
11
The documentation for AspectJ is hosted with Eclipse, of all places. See
http://www.eclipse.org/aspectj/
for de-
tails.
To demonstrate this aspect in action, I need to configure Spring to apply the aspect, and I
need an object to advise. The latter is easy enough. The next listing shows a simple class
with three properties.
Listing 7.15. A simple POJO with three
set
methods
package
mjg;
public class
POJO {
private
String one;
private int
two;
private double
three;
public
String getOne() {
return
one; }
public void
setOne(String one) {
this
.one = one; }
public int
getTwo() {
return
two; }
public void
setTwo(
int
two) {
this
.two = two; }
public double
getThree() {
return
three; }
public void
setThree(
double
three) {
this
.three = three; }
@Override
public
String toString() {
return
"POJO [one=" + one + ", two=" + two +
", three=" + three + "]";
}
}
The class is called
POJO
, and it has three properties, called
one
,
two
, and
three
. Each
has a getter and a setter. The aspect will run before each of the
set
methods.