Java - Spring AOP - 使用before advice停止执行方法?

时间:2014-01-17 14:57:18

标签: java spring aop

如何在Spring AOP中使用之前的建议作为安全措施。

例如,我在建议之前有这个(使用伪代码):

@Before("trigger()")
public void beforeMethod(JoinPoint point){
    Method[] a = point.getSignature().getClass().getDeclaredMethods();
    for(int i=0; i < a.length; i++){
            //if method has such arguments then
        if(a[i] 'has args String name, String role, Int Money'){ 
               //And if these arguments meets such requirements
                if(a[i].argument(Int.class) > 1000 or a[i].argument(String role).equals("normal"))
                    //Stop executing this method
                    a[i].stop
                }
    }
}   

我知道这只是伪代码,所以看起来可能不正确,但我希望你能有所了解。是否有一些JoinPoint方法在符合或不符合某些要求时停止扫描方法?

2 个答案:

答案 0 :(得分:4)

如何抛出异常呢?

if (!isAllowed) {
  throw new NotAuthorizedException(..)
}

答案 1 :(得分:0)

其实你做不到。 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

@Before("trigger()")
public void beforeMethod(JoinPoint point){
    Method[] a = point.getSignature().getClass().getDeclaredMethods();
    for(int i=0; i < a.length; i++){
            //if method has such arguments then
        if(a[i] 'has args String name, String role, Int Money'){ 
               //And if these arguments meets such requirements
                if(a[i].argument(Int.class) > 1000 or a[i].argument(String role).equals("normal"))
                     throw new Exception("Operation Not allowed");
                }
    }
} 
相关问题