14

我正在使用 Java EE 6 和 Jboss AS7.1 并尝试使用拦截器绑定(来自 jboss 站点的示例)。

我有一个 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) {
    /* ... */
}

谢谢你的帮助。

4

3 回答 3

17

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

使用@Priority 注解时,无需在 beans.xml 文件中指定拦截器。

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

它有效。

于 2015-11-19T10:07:15.623 回答
14

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

默认情况下,bean 归档没有通过拦截器绑定绑定的已启用拦截器。必须通过在 bean 存档的 beans.xml 文件的元素下列出其类来显式启用拦截器。

于 2012-08-22T21:23:19.013 回答
4

您可以使用任何优先级值 = 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]

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

于 2016-05-10T13:32:53.023 回答