Javatpoint Logo
Javatpoint Logo

Spring AOP AspectJ Annotation Example

The Spring Framework recommends you to use Spring AspectJ AOP implementation over the Spring 1.2 old style dtd based AOP implementation because it provides you more control and it is easy to use.

There are two ways to use Spring AOP AspectJ implementation:

  1. By annotation: We are going to learn it here.
  2. By xml configuration (schema based): We will learn it in next page.

To understand the aop concepts, its advantage etc. visit here AOP Concepts Tutorial



Spring AspectJ AOP implementation provides many annotations:

  1. @Aspect declares the class as aspect.
  2. @Pointcut declares the pointcut expression.

The annotations used to create advices are given below:

  1. @Before declares the before advice. It is applied before calling the actual method.
  2. @After declares the after advice. It is applied after calling the actual method and before returning result.
  3. @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice.
  4. @Around declares the around advice. It is applied before and after calling the actual method.
  5. @AfterThrowing declares the throws advice. It is applied if actual method throws exception.

Understanding Pointcut

Pointcut is an expression language of Spring AOP.

The @Pointcut annotation is used to define the pointcut. We can refer the pointcut expression by name also. Let's see the simple example of pointcut expression.

The name of the pointcut expression is doSomething(). It will be applied on all the methods of Operation class regardless of return type.

Understanding Pointcut Expressions

Let's try the understand the pointcut expressions by the examples given below:

It will be applied on all the public methods.


It will be applied on all the public methods of Operation class.


It will be applied on all the methods of Operation class.


It will be applied on all the public setter methods of Employee class.


It will be applied on all the methods of Operation class that returns int value.


1) @Before Example

The AspectJ Before Advice is applied before the actual business logic method. You can perform any operation here such as conversion, authentication etc.

Create a class that contains actual business logic.

File: Operation.java

Now, create the aspect class that contains before advice.

File: TrackOperation.java

Now create the applicationContext.xml file that defines beans.

File: applicationContext.xml

Now, let's call the actual method.

File: Test.java

Output

As you can see, additional concern is printed before msg(), m() and k() method is invoked.

Now if you change the pointcut expression as given below:

Now additional concern will be applied for the methods starting with m in Operation class. Output will be as this:

Now you can see additional concern is not printed before k() method invoked.


2) @After Example

The AspectJ after advice is applied after calling the actual business logic methods. It can be used to maintain log, security, notification etc.

Here, We are assuming that Operation.java, applicationContext.xml and Test.java files are same as given in @Before example.

Create the aspect class that contains after advice.

File: TrackOperation.java

Output

You can see that additional concern is printed after calling msg(), m() and k() methods.


3) @AfterReturning Example

By using after returning advice, we can get the result in the advice.

Create the class that contains business logic.

File: Operation.java

Create the aspect class that contains after returning advice.

File: TrackOperation.java
File: applicationContext.xml

It is same as given in @Before advice example

File: Test.java

Now create the Test class that calls the actual methods.

Output

You can see that return value is printed two times, one is printed by TrackOperation class and second by Test class.


4) @Around Example

The AspectJ around advice is applied before and after calling the actual business logic methods.

Here, we are assuming that applicationContext.xml file is same as given in @Before example.

Create a class that contains actual business logic.

File: Operation.java

Create the aspect class that contains around advice.

You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the request by calling the proceed() method.

File: TrackOperation.java
File: Test.java

Now create the Test class that calls the actual methods.

Output

You can see that additional concern is printed before and after calling msg() and display methods.


5) @AfterThrowing Example

By using after throwing advice, we can print the exception in the TrackOperation class. Let's see the example of AspectJ AfterThrowing advice.

Create the class that contains business logic.

File: Operation.java

Create the aspect class that contains after throwing advice.

Here, we need to pass the Throwable reference also, so that we can intercept the exception here.

File: TrackOperation.java
File: applicationContext.xml

It is same as given in @Before advice example

File: Test.java

Now create the Test class that calls the actual methods.

Output








Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA