跟隨我之前的問題的comment。我試圖從@Around
建議中拋出異常,並在被調用類和/或方法中捕獲它。但我發現了這個錯誤:如何捕捉從@Around拋出的連接點中的異常
Stacktraces
java.lang.Exception: User not authorized
com.company.aspect.AuthorizeUserAspect.isAuthorized(AuthorizeUserAspect.java:77)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:616)
...
的看點代碼:
@Aspect
public class AuthorizeUserAspect {
@AuthoWired
private UserService service;
@Pointcut("@annotation(module)")
public void authorizeBeforeExecution(AuthorizeUser module) {}
@Around(value = "authorizeBeforeExecution(module)", argNames="module")
public Object isAuthorized(ProceddingJoinPoint jp, AuthorizeUser module) throws Throwable {
// Check if the user has permission or not
service.checkUser();
if (/* User has NOT permission */) {
throw new MyCustomException("User not authorized"); // => this is line 77
}
return jp.proceed();
}
}
,並在Struts的基於UI操作代碼是:
@Component
public class DashboardAction extends ActionSupport {
@Override
@AuthorizeUser
public String execute() {
...
}
private void showAccessDenied() {
...
}
}
的問題是如何或在哪裏我可以抓住這個例外來執行showAccessDenied()
?
做什麼你的意思是「捕獲任何未捕獲的異常」?來自Application Layer和以下的任何異常? – Khosrow
中斷UI類的任何異常 – lebryant
在執行ui方法之前拋出此異常。不是嗎?但它肯定會打斷它。你會介意提供你的建議代碼嗎? – Khosrow