Introduce about Aspect Oriented Programming

Perhaps in you all, everyone knows and works with Object Oriented Programming (OOP), but with Aspect Oriented Programming (AOP), maybe someone does not know. In this tutorial, I will present the basic concepts of aspect-oriented programming so that you can better understand it.

I do not go into length theory, aspect-oriented programming is kind of programming that allows us to add new processing code to existing applications without having to modify the code of that application.

For example, suppose you have an application, in this application is a class with the following content:

Now, I want to add some code to all the methods of the Application class so that every time the application calls these methods, a log message is printed to the console. With object-oriented programming, we have to modify each of the methods of the Application class to achieve what we want. Aspect-oriented programming defines an easier way to do this without having to modify the code of the Application class.

With aspect-oriented programming, we will find out to each time the methods in the Application class are called, the program must call the code to print out our log message first.

We have many ways to implement the idea of aspect-oriented programming, one of which is to use existing libraries such as Spring AOP, AspectJ, … For more understanding, in this tutorial, I will use the AspectJ library to solve the problem that I have set.

I will create a Maven project as an example:

Introduce about Aspect Oriented Programming

Add AspectJ dependency:

I will add a class with the main() method to use the Application class as follows:

Result:

Introduce about Aspect Oriented Programming

Now, I will use AspectJ to add the log code whenever the sum() and sub() methods in the Application class are called:

First, I will create a new class and use the @Aspect annotation and @Before annotation of AspectJ as follows:

In the above method, we have defined a code that allows us to print out the log message with the content of the method name in the Application class which our program is calling. Here, @Aspect annotation is used to declare for AspectJ that we need to insert code into other classes and they will be declared in this class. I will detail each section about the @Before annotation and its value, about the parameters of the method allMethods(), … in the incoming tutorials.

Next, I will add some Maven plugins to run our application with AspectJ.

Why I did not run the application normally: select the Example class main and then select Run “Example.main()”? That’s because, for the sake of simplicity, we can test the result with just one Maven command 🙂

With aspect-oriented programming, we have three ways of implementing code insertion into an existing class, namely:

  • Compile-Time Weaving: This is the simplest way. At compile source, we will have the code of the class that we want to insert the code and the code that we want to insert. Therefore, we only need to configure to combine these two source code together.
  • Post-Compile Weaving: This method is used to insert code into Java’s .class files that have been compiled.
  • Load-Time Weaving: This method is used to insert code when the class we want to insert is loaded into the JVM.

Specifically each way, I will guide you in the incoming tutorials, in this tutorial I use the simplest way, compile-time weaving.

In order to be able to insert the log message before calling the methods of the Application class, we will use a Maven plugin to do this. If you run it normally, the IDE only builds our class through the .class file to run without AspectJ’s intervention. Therefore, the results will not be as we expected.

The plugin is called aspectj-maven-plugin and I will declare it as follows:

After we have built our application code using AspectJ plugin, we will use another Maven plugin to run the application. That plugin is called exec-maven-plugin and I will declare it like this:

Now, when running the Maven command with the command:

You will see the output as follows:

Obviously, as you can see, the exec-maven-plugin plugin allows us to run the application using the Example class and do what we want:

Of course, use the Maven plugin exec-maven-plugin just to give a simple example for this tutorial. In actual applications, build into jar package to run, we do not need to use this plugin anymore.

 

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment