【问题标题】:AOP - cancel or following execution from adviceAOP - 从通知中取消或执行以下操作
【发布时间】:2021-05-28 19:29:16
【问题描述】:

我正在编写一个注释,它应该在执行函数之前对 MessageId 执行一些验证。

@Aspect
@Component
class ValidatorAspect {
    @Before("@annotation(whatever.Validator)")
    fun checkMessage(joinPoint: JoinPoint) {
        val messageId = joinPoint.args[1] as String
        // run some validations
        // if any validation fails, this should stop execution
        // and annotated function not be called
    }
}

我的函数注释为:

    @Validator
    @SqsListener("myqueue")
    fun run(message: String) {
         // code to proccess message
    }

订单被正确调用:首先 SqsListener 接收消息,然后我的验证器应该在带注释的函数之前执行。

如何从run() 强制停止执行?如果任何验证失败,我不希望调用该带注释的类 - 另外,我不想抛出异常,我正在寻找平滑的东西,停止,没有异常:)

我尝试使用@Around,但我的建议在我的函数run()之后被调用

有什么想法或建议吗?

【问题讨论】:

    标签: java kotlin aop spring-aop


    【解决方案1】:

    @Before 中,如果你想阻止程序进入被拦截的方法,你别无选择,只能抛出异常。显然,您希望避免抛出异常,因此 @Around 是您要在此处使用的建议类型。

    我曾尝试使用 @Around,但我的建议在我的函数 run() 之后被调用

    恕我直言,这是不可能的。您可以在调用连接点之前访问它,这就是它的工作方式。也许您在进行验证之前不小心打电话给joinpoint.proceed()。当然,您必须反其道而行之,首先验证,然后再决定是继续还是只返回替代结果而不继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多