Search code examples
spring-bootaopaspectjspring-aop

spring-boot- @AfterThrowing is not executing advice method though exception is thrown?


I am new to Spring-boot and AOP. I am trying to log the exceptions raised in my spring-boot application. What exactly i am trying to do is whenever any method in my application classes raises the runtime exception, i am logging it to console.

So i created an aspect with @AfterThrowing annotation. To check whether it is working i have intentionally written a line of code which will raise / by zero exception. I tested with it but this advice method is not working.

Following is my test code:

package com.sware.SpringBoot_JPA_MySQL_Gradle_Project.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.sware.SpringBoot_JPA_MySQL_Gradle_Project.domains.User;

@Aspect
@Component
public class UserAspect {


    @Before("execution(* com.sware.SpringBoot_JPA_MySQL_Gradle_Project.services.UserService.saveUserToDb(com.sware.SpringBoot_JPA_MySQL_Gradle_Project.domains.User)) && args(user)")
    public void beforeUserSave(User user) {
       System.out.println("++++++++++++++++++++++++++++++Creating UserBefore Pointcut: \n"+user.toString());

    }



    @After("execution(* com.sware.SpringBoot_JPA_MySQL_Gradle_Project.services.UserService.saveUserToDb(com.sware.SpringBoot_JPA_MySQL_Gradle_Project.domains.User)) && args(user)")
      public void aftereUserSave(User user) {
         System.out.println("++++++++++++++++++++++++++++++Creating User After pointcut: \n"+user.toString());

      }

    @AfterThrowing(pointcut = "execution(* com.sware.SpringBoot_JPA_MySQL_Gradle_Project.services.*.*(..))", throwing = "e")
    public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {

      System.out.println("Okay - we're in the handler...");

      /*Signature signature = joinPoint.getSignature();
      String methodName = signature.getName();
      String stuff = signature.toString();
      String arguments = Arrays.toString(joinPoint.getArgs());
     System.out.println("**************************EXCEPTION: "
          + methodName + " with arguments "
          + arguments + "\nand the full toString: " + stuff + "\nthe exception is: "
          + e.getMessage());*/
    }
}

i have tried multiple permutations of pointcut expression like bellow,but no one works:

@AfterThrowing(pointcut = "execution(* com.sware.SpringBoot_JPA_MySQL_Gradle_Project.services.*.*(..))", throwing = "e")
 @AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "e")  // Application fails to start  throws some internal null pointer exception
 @AfterThrowing(pointcut = "execution(* com.sware.*.*.*.*(..))", throwing = "e")

My Class Code:

  package com.sware.SpringBoot_JPA_MySQL_Gradle_Project.services;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Date;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.sware.SpringBoot_JPA_MySQL_Gradle_Project.domains.User;
    import com.sware.SpringBoot_JPA_MySQL_Gradle_Project.projections.UserWithFnameAndLname;
    import com.sware.SpringBoot_JPA_MySQL_Gradle_Project.repositories.UserRepository;

    @Service
    public class UserService {
        @Autowired
        UserRepository userRepository;
        public List<User> getAllUsers(){

             List<User> userList=new ArrayList<>();
            try {
                Integer n=10/0;
                userList=userRepository.findAll();

            } catch (Exception e) {
                System.out.println("Exception in UserRepostory:"+e.getMessage());
            }
            return userList;
        }
    }

Here, myAfterThrowing method is not getting call though /by zero is thrown at run time. Can anyone tell me where i am going wrong?


Solution

  • Method does not not throw any exception. The "try-catch" block basically absorbs the exception. Move "Integer n=10/0;" outside of the try-catch