AOP @AfterThrowing
Spring Boot AOP has interceptors which can intercept application and its methods. This is to perform some extra action at the time of property initiation, method initialization or destroy.
@AfterThrowing annotation advice get call if any method throws an exception.
- Lets create a project using Spring Initilizr using link
- Fill Group and Artifact detail as com.javadoubts and practice respectively.
- Add dependencies as Spring Web and Spring boot actuator as mentioned in below screenshot.
- information as mentioned in below screenshot. Add dependencies as Spring Web and Spring boot actuator.
3. Unzip the downloaded file and import in Spring Tool Suite. add below below dependencies as part of pom.xml
4. Create UserController.java with below content:
@RestController annotation used to created RESTful web services which can be accessed using http methods like GET, POST, etc. and it is the combination of @Controller and @ResponseBody.
@GetMapping used to map HTTP GET request. It mainly used to fetch/get data from server.
5. Create one more UserServiceAspect.java class. Declare @Aspect and @Component annotation on top of the class.
value=”execution(* com.javadoubts.practice..*(..))”
Above syntax will make sure the advice will get call for every class method inside com.javadoubts.practice package.
6. To consume AOP in Spring boot application it is necessary to declare below highlighted annotation on top of main class as shown below:
7. Right click inside PracticeApplication.java file and Run it as Spring Boot App.
8. Hit below URL and check for console to see the function calling sequence
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.