Spring AOP AspectJ Annotation Example

In this post, we will see Spring AOP AspectJ Annotation examples.
If you are not familiar with Spring AOP terminology, you may go through Spring Aspect oriented programming(AOP) tutorial.
Following are the AspectJ annotations which we are going to use to implement Spring AOP.
@Aspect : To declare a class as Aspect.
Annotations which are used to create Advices are:
@Before : @Before annotation is used to create before advice. It executes before actual method execution (Join points)
@AfterReturning: This annotation is used to create return advice. It executes after a method execution completes without any exception.
@AfterThrowing: This annotation is used to create After throwing advice, it executes if method exits by throwing an exception.
@After : This annotation is used to create after advice, it executes after a method execution  regardless of outcome.
@Around : This annotation is used to create around advice,  It executes before and after a join point.

This may be confusing right now but once you implement an example, it will be clear.

@Before  :

lets say you have some business logic class (BusinessLogic.java) and you want to do logging before execution of getBusinessLogic method of this class.

Now we will create aspect class

execution(* org.arpit.java2blog.BusinessLogic.getBusinessLogic(..)) is pointcut expression. It denotes execution of method loggingBeforeBusinessLogic before exection of getBusinessLogic method of BusinessLogic class.
Lets do xml configuaration of above beans in spring-config.xml.

Create main class named SpringAOPMain to execute the application

When you run above program, you will get below output:

@After:

I am replacing @Before method in LoggingAspect.java with @After annotated method.This method will execute after execution of actual business logic, so LoggingAspect.java will look something like this.

When you run SpringAOPMain.java again, you will get below output:

As you can see in above output, loggingAfterBusinessLogic executed after getBusinessLogic method

@AfterReturning:

I am replacing @After with @AfterReturning in above LoggingAspect.java.This method will execute after execution of actual business logic but only if it successfully returns.

When you run SpringAOPMain.java again, you will get below output:

@AfterThrowing:

Lets add one more method with @AfterThrowing annotation. It will execute if any exception occurs in getBusinessLogic method.
LoggingAspect.java

Now throw RuntimeException from getBusinessLogic to test @AfterThrowing exception. BusinessLogic.java

When you run SpringAOPMain.java , you will get below output:

As you can see loggingAfterBusinessLogicException got executed as there is exception in business logic.

@Around:

Method with @Around will get called before and after execution of business logic.
You need to call reference of ProceedingJoinPoint and call proceed method to execute actual business logic.

When you run SpringAOPMain.java , you will get below output:

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *