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:
web.xml
etc.ServletContextListener
there..Any ideas?
Find a simple solustion for this question if you are using spring-boot:
In the jar, you can define your aspect as a component:
package com.xxx.yy.zz.aspect;
@Aspect
@Component
public class CoolAspect {
// code like the question
}
And the, you need create a file spring.factories
in /resources/META-INF/
.
Spring will scan this file when start.
And the file spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xxx.yy.zz.aspect.CoolAspect
After that, just package them as a jar.
This will work by just declaring a Maven dependency
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With