spring aspectj maven compile time weaving
Is it possible to use spring with aspectj to do compile time weaving and not write the aspectj's .aj file
If there is an example or tutorial that would 开发者_开发问答be great.
This has nothing to do with Spring. AspectJ supports both traditional syntax (.aj files) and the so called @AspectJ
- syntax (.java files with annotations).
Spring AOP only supports the latter, but The AspectJ compiler supports both.
Example:
Java class:
public class ServiceClass{
public void foo(final String bar){
System.out.println(bar);
}
public static void main(final String[] args){
new ServiceClass().foo("Bar Baz Phleem");
}
}
Traditional (.aj) aspect:
public aspect TestTraditionalAspect{
pointcut executeFoo(String bar) : execution(* foo.bar.ServiceClass.foo(**))
&& args(bar)
;
void around(final String args) : executeFoo(args) {
System.out.println("Traditional: Before foo: "+args);
proceed(args);
System.out.println("Traditional: After foo: "+args);
}
}
@AspectJ-style (.java) Aspect (almost equivalent)
@Aspect
public class TestAtAspectJAspect{
@Pointcut(value = "execution(* foo.bar.ServiceClass.foo(**))"
+ " && args(bar)")
public void executeFoo(final String bar){
}
@Around("executeFoo(args)")
public void aroundFooExecution(final String args,
final ProceedingJoinPoint pjp) throws Throwable{
System.out.println("@AspectJ: Before foo: " + args);
pjp.proceed(new String[] { args });
System.out.println("@AspectJ: After foo: " + args);
}
}
Output:
Here's the output when I run ServiceClass.main() as a Java / AspectJ application in Eclipse:
Traditional: Before foo: Bar Baz Phleem
@AspectJ: Before foo: Bar Baz Phleem
bar
@AspectJ: After foo: Bar Baz Phleem
Traditional: After foo: Bar Baz Phleem
Eclipse internally uses the AspectJ compiler, so the same thing will happen if you run the AspectJ compiler on the sources using Maven.
Reference:
Read AspectJ in Action by Ramnivas Laddad for more Info. It also contains info on what kind of Aspects you can use with Spring AOP and what kinds require static compiling. There is no online resource that I know of that even gets close.
Have a look at springsource.org/roo/start. A new product from Spring for rapid application development. You start a roo console and type in your domain object names and attributes and roo creates pojo's which are enhanced with aspectj classes at compile time. Roo creates also an maven pom and at the end you get exactly what you asked for. I found roo perfekt to get an tutorial how to use spring and aspectj and maven and much more.
精彩评论