如果从另一个ejb方法调用ArroundInvoke方法,它是否会被调用?

时间:2011-11-29 13:13:39

标签: ejb-3.0 stateless-session-bean interceptor

我有以下情况:

@Interceptors(MyInterceptClass.class)
public void ejbMethod1()
{

}


@Interceptors(MyInterceptClass.class)
public void ejbMethod2()
{
    ejbMethod1();
}

调用ejbMethod2会导致执行两个拦截器调用吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我假设您的意思是@Interceptors(复数)注释,它定义了在注释方法调用时将调用的拦截器类。 @Interceptor注释(单数)用于注释作为拦截器的类。

如果是这样,那么简短回答是:

拦截器由容器执行。如果您的方法调用不会通过容器,那么<​​strong>将不会被截获。

因此,请致电ejbMethod1()

@Interceptors(MyInterceptClass.class) 
public void ejbMethod2() {
    ejbMethod1(); 
}

不会激活MyInterceptClass,因为它是本地调用(非EJB调用)。

如果你想再次调用拦截器,你应该使用业务接口,如下所示:

// Somewhere in the class
@Resource
SessionContext ctx;

@Interceptors(MyInterceptClass.class) 
public void ejbMethod2() {
    // This is explicit call which will go through the EJB Container
    ctx.getBusinessObject(YourEJBClass.class).ejbMethod1();
}

这将进行EJB感知调用,并在调用ejbMethod1()时命中拦截器。

相关问题