I have java project named Test which have all aspects in it. I wan to use these aspects in another spring boot project. I am working on a poc to weave aspects in Test project into a spring boot application. What is the efficient way to do so and how it can be done, can some one who implemented please suggest.
Code for Java project Test with aspects
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Secured {
public boolean isLocked() default false;
}
@Aspect
public class SecuredMethodAspect {
@Pointcut("@annotation(secured)")
public void callAt(Secured secured) {}
@Around("callAt(secured)")
public Object around(ProceedingJoinPoint pjp, Secured secured) throws Throwable {
if (secured.isLocked()) {
System.out.println(pjp.getSignature().toLongString() + " is locked");
return pjp.proceed();
} else {
return pjp.proceed();
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aspect</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>AspectJ-POC</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
<!-- nickwongdev aspectj maven plugin, this is for weaving of aspects -->
<build>
<plugins>
<plugin>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.12.6</version>
<configuration>
<source>11</source>
<target>11</target>
<complianceLevel>11</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- aspectj runtime dependency -->
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>
Some quick first impressions I got when browsing your projects before cloning them:
delombokin order to first generate source code with Lombok annotations expanded into equivalent source code and then compile the generated sources with AspectJ.Judging from your sample projects and the absence of any
aspectLibrariesandweaveDependenciessections in your AspectJ Maven configuration, you simply did not bother to read any documentation, which leaves me to wonder what you did during the one month you tried to get this working.I would appreaciate a comment, explaining why you want to use CTW instead of LTW. The only reason I can think of is that in addition to your native aspect, you also want to use Spring AOP aspects withing your Spring application. But if you activate native AspectJ LTW, you cannot use Spring AOP at the same time anymore. If that is not an issue, I recommend LTW, as it is documented and tested well in Spring. CTW is no problem either, but more difficult to understand and manage in your POMs.
Update: OK, I did not want to wait any longer for your reason to use CTW and simply decided to show you a LTW solution instead. What I did was:
src/main/resources/org/aspectj/aop.xml, configuring it to use your aspect and target your base package + subpackagesexecution(* com.ak..*(..))with.., not justcom.ak.*.Non-essential cosmetics I applied are:
context.getBean(TestController.class).mainRequest()from the Spring Boot application's main method in order to automatically produce some aspect output, in order to avoid having to usecurlor a web browser in order to call a local URL.try-finallyin order to make sure that the log message after proceeding to the original method is also logged in case of an exception.The diff looks like this (I hope you can read diffs):
For your convenience, here are the complete changed files:
Now you simply make sure to add the JVM argument
-javaagent:/path/to/aspectjweaver-1.9.6.jarto your Java command line in order to activate native AspectJ LTW. For more details about how to configure LTW within Spring if more sophisticated ways, please read the Spring manual, section Using AspectJ with Spring Applications.The console log should look something like this: