spring-boot- @AfterThrowing 抛出异常但未执行建议方法?
spring-boot- @AfterThrowing is not executing advice method though exception is thrown?
我是 Spring-boot 和 AOP 的新手。我正在尝试记录在我的 spring-boot
应用程序中引发的异常。我真正想做的是,每当我的应用程序 类 中的任何方法引发 运行time 异常时,我都会将其记录到控制台。
所以我创建了一个带有 @AfterThrowing
注释的方面。为了检查它是否正常工作,我特意编写了一行代码,它会引发 / by zero
异常。我用它测试过,但这个建议方法不起作用。
以下是我的测试代码:
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());*/
}
}
我试过像下面这样的切入点表达式的多种排列,但没有一个有效:
@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")
我的Class代码:
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;
}
}
此处,myAfterThrowing
方法未被调用,尽管 /by zero
在 运行 时抛出。谁能告诉我哪里出错了?
方法没有抛出任何异常。 "try-catch" 块基本上吸收了异常。将 "Integer n=10/0;"
移到 try-catch
之外
我是 Spring-boot 和 AOP 的新手。我正在尝试记录在我的 spring-boot
应用程序中引发的异常。我真正想做的是,每当我的应用程序 类 中的任何方法引发 运行time 异常时,我都会将其记录到控制台。
所以我创建了一个带有 @AfterThrowing
注释的方面。为了检查它是否正常工作,我特意编写了一行代码,它会引发 / by zero
异常。我用它测试过,但这个建议方法不起作用。
以下是我的测试代码:
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());*/
}
}
我试过像下面这样的切入点表达式的多种排列,但没有一个有效:
@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")
我的Class代码:
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;
}
}
此处,myAfterThrowing
方法未被调用,尽管 /by zero
在 运行 时抛出。谁能告诉我哪里出错了?
方法没有抛出任何异常。 "try-catch" 块基本上吸收了异常。将 "Integer n=10/0;"
移到 try-catch