Java 离开方法时使用拦截器

Java 离开方法时使用拦截器,java,jakarta-ee,interceptor,Java,Jakarta Ee,Interceptor,在我的JavaEE程序中,我想使用拦截器进行日志记录。当我输入方法时,它很容易使用: 注释: @Inherited @InterceptorBinding @Retention(RUNTIME) @Target({ METHOD, TYPE }) public @interface Logged { } 拦截器: @Logged @Interceptor public class LoggedInterceptor implements Serializable { private

在我的JavaEE程序中,我想使用拦截器进行日志记录。当我输入方法时,它很容易使用:

注释:

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({ METHOD, TYPE })
public @interface Logged {

}
拦截器:

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private Logger logger;

    @AroundInvoke
    public Object logMethodEntry(InvocationContext invocationContext) throws Exception {

        logger.info("Entering method: "
            + invocationContext.getMethod().getName() + " in class "
            + invocationContext.getMethod().getDeclaringClass().getName());

        return invocationContext.proceed();

    }
}
public class MyClass {

    @Logged
    public void MyMethod() {
        // do something
    }

}
我的类使用拦截器:

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private Logger logger;

    @AroundInvoke
    public Object logMethodEntry(InvocationContext invocationContext) throws Exception {

        logger.info("Entering method: "
            + invocationContext.getMethod().getName() + " in class "
            + invocationContext.getMethod().getDeclaringClass().getName());

        return invocationContext.proceed();

    }
}
public class MyClass {

    @Logged
    public void MyMethod() {
        // do something
    }

}

但现在我想在离开我的方法时做同样的事情。这是可能的吗?

AroundInvoke并不意味着特别地输入-它意味着你“在调用周围”挂起它;它的名字选得很恰当。其中的procedure()调用是您正在使用拦截器包装的实际方法调用。因此,您当前在继续()调用之前记录日志-如果您在继续()调用之后添加日志,那么这就是离开方法调用的点

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private Logger logger;

  @AroundInvoke
  public Object logMethodCall(InvocationContext invocationContext) throws Exception {

        logger.info("Entering method: "
          + invocationContext.getMethod().getName() + " in class "
          + invocationContext.getMethod().getDeclaringClass().getName());

        Object ret = invocationContext.proceed();

        logger.info("Left method: "
          + invocationContext.getMethod().getName() + " in class "
          + invocationContext.getMethod().getDeclaringClass().getName());

        return ret;
  }
}

@吉姆比的回答几乎是正确的。他的解决方案缺少的是异常处理。如果出现异常,“Left方法”永远不会被记录

建议集解决方案:

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private Logger logger;

  @AroundInvoke
  public Object logMethodCall(InvocationContext invocationContext) throws Exception {

        Object ret = null;
        logger.info("Entering method: "
          + invocationContext.getMethod().getName() + " in class "
          + invocationContext.getMethod().getDeclaringClass().getName());

        try {

           ret = invocationContext.proceed();

        } catch(Exception e) {

            throw e;

        } finally  {

           logger.info("Left method: "
             + invocationContext.getMethod().getName() + " in class "
             + invocationContext.getMethod().getDeclaringClass().getName());

        }

        return ret;
  }
}

在这段代码中,如何在调用context.procedure()成功后获得主函数的响应