EJB 上基于注解的拦截器

Annotation-based interceptors on EJBs

是否可以像我们在 CDI bean 上那样使用可绑定拦截器的注释在 EJB 上声明拦截器?

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface MyInterceptor {
}

@Stateless
@Remote(MyService.class)
public MyServiceImpl implements MyService {

     @Override
     @MyInterceptor
     public String myBusinessMethod() {
          return "";
     }

}

是的,这是可能的。您几乎可以像对待任何其他 CDI bean 一样对待 EJB。您需要激活 beans.xml 中的拦截器或添加 @Interceptor 绑定。有关详细信息,请参阅焊接文档 https://docs.jboss.org/weld/reference/1.0.0/en-US/html/interceptors.html

标准的 EJB 注释也是这样实现的,可以在 src 或上述文档中看到 link。