I want to have a JAR with an aspect that intercepts all method calls, e.g.
@Aspect
public class CoolAspect {
@Pointcut("execution(public * *(..))")
public void anyPublicMethod() { }
@Before("anyPublicMethod()")
public void advice(JoinPoint point) {
System.out.println("sign:\t" + point.getSignature());
}
}
Suppose the above is the aspect I have and want clients to use. I compile it to a JAR and make available to Maven.
Now the clients would use this jar as a dependency, e.g.
<dependency>
<groupId>cool-group</groupId>
<artifactId>cool-artifact</artifactId>
<version>1.0.0</version>
</dependency>
This artifact (JAR) has the mentioned aspect.
Now is it possible for the aspect work by just declaring a Maven dependency?
Few things that might be important:
- I plan to use AspectJ (perhaps Spring AOP, if necessary),
- The clients will probably be web applications with normal
web.xml
etc. - Clients are built with Maven
- I want the Clients to be as easy to configure as possible - in my original idea a Maven dependency would be enough.
- The "Annotation JAR" will contain a web-fragment, so it's possible to declare some custom
ServletContextListener
there..
Any ideas?
No, this is impossible, because AspectJ must be started prior to any client classes. All client classes should be loaded through a special enhanced class loader, which will process annotations.
The other thing is Spring's AOP. it weaves beans during application context start up and does not require special class loader. That would be the easiest way for client if he uses spring. Add maven dependency and then configure AOP in the application context.
There are four types of weaving: