【发布时间】:2021-08-18 08:57:16
【问题描述】:
当此方法出现异常时,我想执行一些操作。 所以我做了一个类似AfterReturning、AfterThrowing的aop函数……
我故意更改了引发异常的方法。 但是请求到达AfterReturning,而不是AfterThrowing,即使发生异常。
如何让这个请求到达 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");
}
请帮助我。 谢谢!
【问题讨论】: