Javatpoint Logo
Javatpoint Logo

Spring Boot AOP After Throwing Advice

After throwing is an advice type in Spring AOP. It ensures that an advice runs if a method throws an exception. We use @AfterThrowing annotation to implement the after throwing advice.

Syntax:

Where:

PointCut: It selects a function.

execution(expression): It is an expression on which advice is to be applied.

throwing: The name of the exception to be returned.

Let's implement the after throwing advice in an application.

Spring Boot After Throwing Advice Example

We will use the previous example in this section. You can download the project or make some modifications in the previous example.

Spring Boot After Throwing Advice Example

Step 1: Open Spring Initializr http://start.spring.io.

Step 2: Provide the Group name. We have provided the Group name com.javatpoint.

Step 3: Provide the Artifact Id. We have provided the Artifact Id aop-after-throwing-advice-example.

Step 4: Add the Spring Web dependency.

Step 5: Click on the Generate button. When we click on the Generate button, it wraps all the specifications in a jar file and downloads it to the local system.

Step 6: Extract the downloaded jar file.

Step 7: Import the folder by using the following steps:

File -> Import -> Existing Maven Projects -> Next -> Browse the Folder aop-after-throwing-advice-example -> Finish.

Step 8: Open the pom.xml file and add the following AOP dependency. It is a starter for aspect-oriented programming with Spring AOP and AspectJ.

pom.xml

Step 9: Create a package with the name com.javatpoint.model in src/main/java folder.

Step 10: Create a class with the name Account in the package com.javatpoint.model.

In the Account class, do the following:

  • Defined two variables accountNumber and accountType of type String.
  • Right-click on the file -> Source -> Generate Constructor using Fields
  • Generate Getters.
    Right-click on the file -> Source -> Generate Getters and Setters -> Select Getters -> Generate
  • Generate a toString()
    Right-click on the file -> Source -> Generate toString()

Account.java

Step 11: Create another package with the name com.javatpoint.service.impl.

Step 12: In this package, create a class with the name AccountServiceImple.

In this class, we have defined account service.

AccountServiceImpl.java

Step 13: Create an interface with the name AccountService in the package com.javatpoint.service.impl.

AccountService.java

Step 14: Create a package with the name com.javatpoint.aspect.

Step 15: Create a class with the name AccountAspect in the package com.javatpoint.aspect.

In this class, we have implemented the after throwing advice by using the annotation @AfterThrowing. We have also defined a method afterThrowingAdvice() method.

Note: The name (ex) that we define in the throwing attribute must correspond to the name of a parameter in the advice method. Otherwise, advice will not run.

AccountAspect.java

Step 16: Open the AopAfterThrowingAdviceExampleApplication.java file and add an annotation @EnableAspectJAutoProxy.

The annotation enables support for handling components marked with AspectJ's @Aspect annotation. It is used with @Configuration annotation.

We have used the proxyTargetClass attribute of the annotation @EnableAspectJAutoProxy. The attribute proxyTargetClass=true allows us to use CGLIB (Code Generation Library) proxies instead of the default interface-based JDK proxy approach.

ConfigurableApplicationContext is an interface that provides facilities to configure an application context in addition to the application context client methods in the ApplicationContext.

AopAfterThrowingAdviceExampleApplication.java

After creating all the class and packages, the project directory looks like the following:

Spring Boot AOP After Throwing Advice

Step 17: Open the AopAfterThrowingAdviceExampleApplication.java file and run it as Java Application. It shows the output, as shown below:

Spring Boot AOP After Throwing Advice

Next TopicSpring Boot JPA




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA