如何在java工具中使用拦截器?

时间:2013-11-28 13:45:12

标签: java interceptor

是否可以在没有应用程序服务器的情况下使用拦截器,例如仅在命令行工具中使用?

我尝试了以下内容,但我没有看到任何输出:

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class PerformanceInterceptor {
    @AroundInvoke
    Object measureTime(InvocationContext ctx) throws Exception {
        System.out.println("STARTING"); //I don't see this
    }
}


class MyTest() {
    @Interceptors(PerformanceInterceptor.class)
    void execute() {
        System.out.println("RUNNING"); //I see this
    }
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

请查看此示例:http://www.byteslounge.com/tutorials/java-ee-ejb-interceptors-tutorial-and-example。 BTW,很棒的Java EE 6功能。

没有EJB和Java EE员工在Java中使用动态代理。