we enable AspectJ for the project, an error will be reported by the AspectJ tool. To have AspectJ tooling and
the bean() pointcut designator coexist, there are two options:
For the bean() pointcut designator, use the aop namespace in the XML
·
configuration for declaration instead of the @AspectJ-style annotation.
Do not use bean() pointcut designator when you want to use AspectJ in your
·
project.
To get rid of the error, just delete the MyAdvice class at the moment.
Listing 7-32 shows the MessageWrapper aspect (the filename is MessageWrapper.aj, which is an
AspectJ file instead of a standard Java class). The aspect was created using the New Aspect option in
STS, as indicated in Figure 7-6.
Listing 7-32. MessageWrapper Aspect
package com.apress.prospring3.ch7.aspectj;
public aspect MessageWrapper {
private String prefix;
private String suffix;
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
pointcut doWriting() :
execution(*
com.apress.prospring3.ch7.aspectj.MessageWriter.writeMessage());
before() : doWriting() {
System.out.println(prefix);
}
after() : doWriting() {
System.out.println(suffix);
}
}
Much of this code should look familiar. Essentially we create an aspect called MessageWrapper, and,
just like a normal Java class, we give the aspect two properties, suffix and prefix, which we will use when
advising the writeMessage() method. Next, we define a named pointcut, doWriting(), for a single
joinpoint, in this case, the execution of the writeMessage() method. (AspectJ has a huge number of
joinpoints, but coverage of those is outside the scope of this example.) Finally, we define two lots of
advice: one that executes before the doWriting() pointcut and one that executes after it. The before
advice writes a line containing the prefix, and the after advice writes a line containing the suffix. Listing
7-33 shows how this aspect is configured in Spring (aspectj.xml).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home