Spring AOP – @After Advice

Facebooktwitterredditpinterestlinkedinmailby feather

Spring AOP – @After Advice

We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice that executes regardless of method outcome (normal or exceptional return).

Let’s understand the concept of after advice briefly with the help of an example.

Example of @After Advice

Here, we are going to create an example of after advice using @After annotation.

Following are the steps to create an example of @After:

  • Create a bean

In this step, we are going to create a bean named Account, that contains the bean object.

Account.java

  • Create a DAO class

In this step, we are going to create a DAO class with the name StudentAccountDao, that contains the business logic of the application.

StudentAccountDao.java

  • Create a class that performs component scanning

In this step, we are going to create a class with the name DemoAOP, that performs the component scanning.

DemoAOP.java

  • Create an aspect class for aspect logic

In this step, we are going to create a class with the name AspectClass, that contains the aspect logic (types of advices).

AspectClass.java

  • Create the main class

In this step, we are going to create the main class with the name App, that contains the main method.

App.java

Output

The following output shows that the @After advice is executed on the method containing exception.

After advice is executed

The following output shows that the @After advice is executed on the method without exception.

Create an aspect class for aspect logic
Facebooktwitterredditpinterestlinkedinmailby feather