@Before annotation in AspectJ

In previous tutorials about introducing aspect oriented programming and compiling-time weaving with AspectJ, I introduced you all to the @Before annotation and @After annotation to insert the code before and after when our application call to a method of an object. They are the Advice. In addition to @Before annotation and @After annotation, we also have other Advice like @AfterReturn annotation, @AfterThrowing annotation, and @Around annotation. In this tutorial, I will introduce you all more about the @Before annotation then you can have a better understanding, know when to use it.

First, I will also create a new project as an example:

@Before annotation in AspectJ

AspectJ dependency:

I will run the example using compile-time weaving, so I will declare the plugin aspectj-maven-plugin as follows:

Class need insert code:

Application class:

Now, we will define a class to insert the code which we want before the HelloWorld object ‘s hello() method using the AspectJ’s @Before annotation.

HelloWorldAspect class has the following contents:

Here, I have defined a method in the HelloWorldAspect class named allMethods() with the @Before annotation. The content of the @Before annotation is as follows:

As you can see, @Before annotation defines two attributes: value and argNames where argNames is optional. The value attribute is the most important attribute, it defines a Pointcut, which helps us to define exactly where we want to insert the code.

We can define a Pointcut using a Combinator along with a Pattern expression.

A Pattern expression is defined by the following elements:

All of these elements can be declared with “*” meaning any value.

Inside:

  • modifiers: defines the access modifier of the method to which we want to insert the code. We can leave it blank, it has the same meaning as the “*” value.
  • return type: void or any type of data of the method which we need to insert code, return.
  • (packageName) (className) (methodName): Define the method which we want to insert the code. In my example, since I was trying to insert code before the HelloWorld object’s hello() method then I defined com.huongdanjava.aspectj.HelloWorld.hello. You can also replace it with com.huongdanjava.aspectj.HelloWorld.* to insert code for all methods in the HelloWorld object.
  • parameters: Define the method with the parameter we need to insert the code. We can use (..) to specify a method with any parameter.

In the above components, each component acts as a criterion to specify the location where we want to insert the code.

The Combinator defines when our code needs to be inserted, after the pattern has been satisfied, is executed. AspectJ defines a list of combinators that you can use for your purposes, here. Each combinator can be defined with different patterns, allowing us to define when the code we need to insert should be executed.

As above example, we use the “execution” combinator to execute the code when the program calls to methods in the HelloWorld object.

The result when we run the above example is as follows:

@Before annotation in AspectJ

To get all the information about the method we are inserting the code, you can use the JoinPoint object as an argument in the method defined with the @Before annotation as follows:

In the code above, I used the JoinPoint object to get the name of the method being executed.

Result:

@Before annotation in AspectJ

Using JoinPoint we can do many other things, we will learn more about it when discussing the other Advice.

 

5/5 - (1 vote)

Add Comment