Different Types of AspectJ Weaving

Axellageraldinc Adryamarthanino
4 min readMay 3, 2021

--

Photo by Chris Chow on Unsplash

There are different types of weaving on AspectJ, I’m going to share the differences for each weaving types .

For you who wants to learn about the basics of AspectJ, this is a good article to read > https://www.baeldung.com/aspectj
But if you have grasped the basic concepts of AOP (moreover AspectJ) and wants to know the details about different types of AspectJ weaving, which are Compile-time weaving, Post-compile (binary) weaving, and Load-time weaving, do continue reading this article.

Compile-time Weaving

Compile-time weaving process
Compile-time weaving (https://www.manning.com/books/aspectj-in-action-second-edition)

The weaving process in compile-time weaving happens (obviously) at compile time. As you can see from the diagram above, the left-hand side describes our source codes which are java files, java classes with @Aspect annotation, and the last one are traditional aspect classes. They are then compiled by ajc (AspectJ Compiler) to be woven into compiled class called woven system. To give more perspective on this, take a look at several code snippets below.

Target class to be woven
Before Advice

Those 2 code snippets are just regular steps to do if we want to do AOP. The 2 snippets above mean that we want to advice greet() method (which resides inside Target class) with before advice. Nothing fancy happens in the aspect, it’ll just print some information about the method invocation.

Now, this is where something is getting interesting. Let’s define a plugin inside our pom.xml

AspectJ Maven Plugin

The AspectJ maven plugin stated above will weave the aspects when we execute mvn clean compile. Now, let’s try to do just that and see what happen.
If you see inside the target directory (which consists of every compiled classes) and open Target.class, you’ll see this.

Target class woven using CTW (Compile-time Weaving)

You see that on line 7–8, the compiler inserts additional functionality which calls the aspect we defined before. This way, the before advice will be executed before the actual process done by the target. Now you know how CTW works internally.

Post-compile (Binary) Weaving

Post-compile (Binary) Weaving
Post-compile (Binary) Weaving (https://www.manning.com/books/aspectj-in-action-second-edition)

Basically, binary weaving is similar to CTW (Compile-time Weaving), weaving process is also done on compile-time. The difference is that with Binary Weaving, we’re able to weave aspects into 3rd party library’s source code. Let’s take a look at code snippets below.

Add new library as dependency to our main project’s pom.xml
Advising greetFromLib() method which exists inside our 3rd party library added previously

At this point, we know that greetFromLib() exists inside our 3rd party library, which means in form of .jar file instead of our own written source codes. We need to do several modifications on our AspectJ Maven plugin to accomodate this.

Adding our third party lib into weave dependency to be woven

Now that we’ve added necessary information to our AspectJ maven plugin, we simply just have to execute mvn clean compile again, and let’s see what changes inside our target directory.

TargetLib class woven using Binary Weaving

Similar as what we’ve observed from CTW, the TargetLib class (in which the source code exists on 3rd party library, we don’t host the source code in our main project) got woven by Binary Weaving by using similar mechanism.

Load-time Weaving

Load-time Weaving
Load-time Weaving (https://www.manning.com/books/aspectj-in-action-second-edition)

Load-time weaving happens when the classes are about to be loaded into JVM. This means that after compilation, nothing will be added into our compiled classes (unlike CTW and Binary Weaving).

  • Deploy an application
  • VM initializes the weaving agent
  • Weaving agent loads all aop.xml files (Yes, we can define multiple aop.xml files and everything gets loaded)
  • Weaving agent loads listed aspects in aop.xml files
  • System starts normal execution
  • VM loads classes during execution (as usual)
  • The VM notifies the weaving agent whenever it loads a class
  • The weaving agent (after being notified), inspects the to-be-loaded class to determine if any of the aspects need to be woven to the to-be-loaded class
  • If so, the weaving agent will weave the class & the aspect
  • The woven byte code will be loaded to VM and used

Comparison Between Different Types of Weaving

Based on this research paper, this is the comparison results (compared to No-AOP implementation).

CTW and LTW compared to No-AOP implementation
  • Execution time of CTW is slightly faster than LTW. In my opinion, it’s because for CTW, the weaving process happened on compile-time, meaning that there is no operation overhead on runtime to do the weaving.
  • CPU usage for CTW is also slightly lower than LTW. In my opinion, the reason is the same as the first point above.
  • Memory usage for CTW is considered pretty significantly lower than LTW. In my opinion, the reason is the same as the other 2 points above.

Conclusion

I hope with this article, you guys can have a more deeper overview about different types of weaving, specifically on AspectJ.

--

--

No responses yet