What is AspectJ?

As we know from the start of this chapter, AOP is a programming paradigm that helps to decouple our code by separating the implementation of crosscutting concerns. AspectJ is the original implementation of AOP, which implements both concerns and the weaving of crosscutting concerns using extensions of Java programming language.

To enable AspectJ in our project, we need AspectJ libraries and AspectJ provides different libraries based on its usage. One can find all its libraries, at https://mvnrepository.com/artifact/org.aspectj.

In AspectJ, Aspects will be created in a file with the extension .aj. The following is the sample TransferAspect.aj file:

public aspect TransferAspect {
pointcut callTransfer(Account acc1, Account acc2, int amount) :
call(public * TransferService.transfer(..));

boolean around(Account acc1, Account acc2, int amount) :
callTransfer(acc1, acc2,amount) {
if (acc1.balance < amount) {
return false;
}
return proceed(acc1, acc2,amount);
}
}

To enable compile-time weaving, when we have both aspect code and code to which we want to weave aspects, use the Maven plugin as follows:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
</configuration>
<executions>
<execution>
<goals>
<!-- use this goal to weave all your main classes -->
<goal>compile</goal>
<!-- use this goal to weave all your test classes -->
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

To perform post-compile time weaving, when we want to weave existing class files and JAR files, use Mojo's AspectJ Maven plugin as follows. The artifact or JAR files we reference to weave must be listed as <dependencies/> in the Maven project and listed as <weaveDependencies/> in the <configuration> of the AspectJ Maven plugin. The following is the Maven sample for how to define weaving dependencies:

<configuration>
<weaveDependencies>
<weaveDependency>
<groupId>org.agroup</groupId>
<artifactId>to-weave</artifactId>
</weaveDependency>
<weaveDependency>
<groupId>org.anothergroup</groupId>
<artifactId>gen</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>

To perform Load-time weaving (LTW), when we want to defer our weaving until the class loader loads a class file, we would need a weaving agent; use the Maven plugin as follows:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/aspectj/
aspectjweaver/${aspectj.version}/
aspectjweaver-${aspectj.version}.jar
</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
<forkMode>always</forkMode>
</configuration>
</plugin>

For LTW, it looks for aop.xml in the classpath under the META-INF folder. The file contains the aspect and weaver tags as follows:

<aspectj>
<aspects>
<aspect name="com.packt.springhighperformance.ch3.bankingapp.
aspectj.TransferAspect"/>
<weaver options="-verbose -showWeaveInfo">
<include
within="com.packt.springhighperformance.ch3.bankingapp
.service.impl.TransferServiceImpl"/>
</weaver>
</aspects>
</aspectj>

So this was just an introduction for how to enable AspectJ in our project.