【问题标题】:Interceptor method not called with interceptor binding未使用拦截器绑定调用拦截器方法
【发布时间】:2012-08-18 01:56:36
【问题描述】:

我正在使用 Java EE 6 和 Jboss AS7.1 并尝试使用拦截器绑定 (Example from jboss site)。

我有一个 InterceptorBinding 注释:

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

拦截器:

@GeoRestrictedEquipment
@Interceptor
public class GeoRestrictedEquipmentInterceptor {

    @EJB EquipmentDao equipmenttDao;    
    @EJB SecurityService securityService;    

    @AroundInvoke
    public Object checker(InvocationContext ctx) throws Exception {
        Integer id = (Integer) ctx.getParameters()[0];
        Equipment equipment = equipmenttDao.findById(id);
        GeoChecker.check(equipment.getSite(), securityService.getUser());

        return ctx.proceed();
    }
}

还有一颗豆子:

@Stateless
@LocalBean
@SecurityDomain(Realm.NAME)
@RolesAllowed({ Roles.REGISTERED })
public class PumpService implements PumpServiceLocal {

    @Override
    @GeoRestrictedEquipment
    public PumpInfos getPumpInfos(Integer pumpId) {
        /* ... */
    }
}

但是没有调用拦截器...我从示例中遗漏了什么?

在我写这个的时候调用了拦截器:

@Override
@Interceptors({GeoRestrictedEquipmentInterceptor.class})
public PumpInfos getPumpInfos(Integer pumpId) {
    /* ... */
}

感谢您的帮助。

【问题讨论】:

    标签: java ejb java-ee-6 interceptor


    【解决方案1】:

    根据文档,还有另一种方法,而不是使用 beans.xml:

    你不需要在 beans.xml 文件中指定拦截器 你使用@Priority 注释。

    @Logged
    @Interceptor
    @Priority(Interceptor.Priority.APPLICATION)
    public class LoggedInterceptor implements Serializable { ... }
    

    它有效。

    【讨论】:

      【解决方案2】:

      您是否按照参考示例中的说明启用了拦截器?

      默认情况下,一个 bean 归档没有通过绑定启用的拦截器 拦截器绑定。拦截器必须通过以下方式显式启用 在 beans.xml 的元素下列出它的类 bean 档案的文件。

      【讨论】:

      • 非常感谢,我阅读示例太快了,我认为使用注解不是必须使用 beans.xml,但实际上它是......
      • JavaEE7 似乎也是如此。
      【解决方案3】:

      您可以使用任何优先级值 = Priority.Application 默认为 2000。

      例如 =

      @Interceptor 
      @Loggable 
      @Priority(100)
      public class FileLogger {}
      

      优先类型:

      • PLATFORM_BEFORE [0-999] - 拦截器从第一个开始。它们由平台启动。
      • LIBRARY_BEFORE [1000-1999] - 由图书馆提供。
      • APPLICATION [2000-2999]- 由应用程序
      • LIBRARY_AFTER,PLATFORM_AFTER [3000-4000]

      您管理拦截器的主要加载。

      【讨论】:

        猜你喜欢
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        相关资源
        最近更新 更多