JAVA aop @AfterThrowing无法捕获异常



当这个方法出现异常时,我想执行一些操作。所以我做了一个aop函数,比如AfterReturn,AfterThrough。。。

我故意改变了引发异常的方法。但是请求到达AfterReturning,而不是AfterThroung,即使发生异常。

如何使此请求达到AfterThrowing方法?

@EventListener
@GetHost
public void getHost(ContextRefreshedEvent event) throws Exception {
try {
prHostName = InetAddress.getLocalHost().getHostName();
prIpAddr = InetAddress.getLocalHost().getHostAddress();
//making exception on purpose
String[] tmp = new String[0];
prIpAddr = tmp[1];

} catch (Exception exception) {
prHostName = "unknown";
prIpAddr = "unknown";
System.out.println("unknown");
exception.printStackTrace();
}
}

有aop方法

@Pointcut("@annotation(com.etc.web.annotation.GetHost)")
public void componentMethod() {}
@Before("componentMethod()")
public void beforeComponent(JoinPoint point) {
System.out.println("beforeComponent");
}
@Around("componentMethod()")
public Object aroundComponent(ProceedingJoinPoint point) throws Throwable {
System.out.println("aroundComponent");
Object retVal = point.proceed();
return retVal;
}
@AfterReturning(pointcut = "componentMethod()", returning = "result")
public void afterComponent(JoinPoint point, Object result) {
System.out.println("afterReturnComponent");
}
@AfterThrowing(pointcut = "componentMethod()", throwing = "exception")
public void afterThrowingComponent(JoinPoint point, Throwable exception) throws Exception {
System.out.println("afterThrowingComponent");

}

请帮帮我。谢谢

该方法不会带异常退出,因为异常被捕获并且该方法正常退出。因此,@AfterThrowing当然不会被触发。这应该不会让你感到惊讶。

相关内容

  • 没有找到相关文章

最新更新