![](/img/trans.png)
[英]Spring AOP: Why @After annotation executes after @AfterThrowing in Java, AspectJ?
[英]JAVA aop @AfterThrowing couldn't catch Exception
當此方法出現異常時,我想執行一些操作。 因此我做了aop function 像AfterReturning,AfterThrowing ...
我故意更改了引發異常的方法。 但是請求到達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");
}
請幫我。 謝謝!
該方法不會因異常而退出,因為異常被捕獲並且該方法正常退出。 所以當然, @AfterThrowing
不會被觸發。 這不應該讓你感到驚訝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.