Java AOP-取消或跟随通知执行
我正在编写一个注释,它应该在执行函数之前对MessageId执行一些验证Java AOP-取消或跟随通知执行,java,kotlin,aop,spring-aop,Java,Kotlin,Aop,Spring Aop,我正在编写一个注释,它应该在执行函数之前对MessageId执行一些验证 @Aspect @Component class ValidatorAspect { @Before("@annotation(whatever.Validator)") fun checkMessage(joinPoint: JoinPoint) { val messageId = joinPoint.args[1] as String // run s
@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接收消息,然后我的验证器应该在带注释的函数之前执行
@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
}
}
如何从run()
强制停止执行?如果任何验证失败,我不希望调用带注释的类-而且,我不希望抛出异常,我正在寻找平滑的东西,只要停止,没有异常:)
我曾尝试使用@Around
,但在我的函数run()
有什么想法或建议吗?在
@Before
中,如果您想阻止程序进入拦截的方法,除了抛出异常之外,别无选择。显然,您希望避免抛出异常,因此这里要使用的建议类型是@Around
我曾尝试使用@Around
,但在我的函数run()
恕我直言,这是不可能的。您可以在调用连接点之前访问它,这就是它的工作方式。可能您在执行验证之前意外调用了joinpoint.continue()
。当然,您必须以另一种方式进行验证,首先验证,然后决定是否要继续,或者只是返回一个替代结果而不继续