aroundInvoke:奇怪的名字?

Ven*_*uri 2 java interceptor cdi

请求有关此拦截器注释的更多信息。为什么拦截器注解叫@AroundInvoke而不是@BeforeInvoke?

例如,我可以在操作 API 之前调用访问验证吗?如何保证在实际调用方法之前完成访问验证?VM 或 CDI 实现是否会执行某些操作,但不会阻止实际调用,而是并行执行此拦截器?

如果我使用Google Guice AOP 方法拦截器,我确信访问验证失败将确定方法调用是否开始。我如何确保雅加达 CDI 也会采取类似的行为?

无法在规范中找到此信息

可以在这里找到相关问题。但上述具体问题仍未得到解答。

Gen*_*ger 5

@AroundInvoke之所以这样称呼,是因为它可以在实际调用的方法之前和之后执行。查看文档及其示例:

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception { ... }
Run Code Online (Sandbox Code Playgroud)

在该方法中,您可以ctx.proceed()调用目标方法(或任何其他拦截器)。当您在该调用之前执行操作时,您将在该方法之前执行操作,而在该调用之后执行的任何操作都将在调用该方法之后发生。因此它是“围绕”该方法的。

样本:

@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
   log.info("We're about to do the thing!");
   Object result = ctx.proceed();
   log.info("We did the thing!");
   return result;
}
Run Code Online (Sandbox Code Playgroud)