我是Spring-boot和AOP的新手。我正在尝试记录我的spring-boot
应用程序中引发的异常。我想要做的是当我的应用程序类中的任何方法引发运行时异常时,我将其记录到控制台。
所以我用@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());*/
}
}
我尝试了多种切入点表达方式,如bellow,但没有人工作:
@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")
我的班级代码:
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
方法没有得到调用。谁能告诉我哪里出错了?
答案 0 :(得分:0)
方法不会抛出任何异常。 “try-catch”块基本上吸收了异常。将"Integer n=10/0;"
移到try-catch