拦截CDI bean中的私有方法

安那托利

我有一个带有私有方法的CDI bean。我写了一个如下的拦截器:

拦截器代码:

@Interceptor
@TimeMeasure
public class TimeWatcher implements Serializable {

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

        long t0 = System.nanoTime();
        try {
            return ctx.proceed();
        } finally {
            long dt = System.nanoTime() - t0;
            System.out.println("Method execution time: " + ctx.getMethod().getName() +  " - " + dt);
        }
    }
}

注释的代码:

 import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;


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

    }

一切都只适用于在外部调用的公共方法,如果我从CDI bean内部调用方法,则无法正常工作。我将JSF 2.0 MyFaces和Omnifaces结合使用来完成@ViewScoped

先感谢您。

亚历山大·朗格

这是设计使然。内部呼叫永远不会被拦截。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章