没有使用拦截器绑定调用拦截器方法
我正在使用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) { /* ... */ }
谢谢你的帮助。
您是否按照引用示例中的描述启用了拦截器?
默认情况下,bean归档文件没有通过拦截器绑定绑定的已启用拦截器。 必须通过在bean归档文件的beans.xml文件的元素下列出其类来显式启用拦截器。
根据文档,还有另一种方法,而不是使用beans.xml:
使用@Priority批注时,无需在beans.xml文件中指定拦截器。
@Logged @Interceptor @Priority(Interceptor.Priority.APPLICATION) public class LoggedInterceptor implements Serializable { ... }
它有效。
您可以使用任何优先级值= Priority.Application默认为2000。
例如=
@Interceptor @Loggable @Priority(100) public class FileLogger {}
优先类型:
- PLATFORM_BEFORE [0-999] – 拦截器从第一个开始。 它们由平台启动。
- LIBRARY_BEFORE [1000-1999] – 由图书馆提供。
- 申请[2000-2999] – 申请
- LIBRARY_AFTER,PLATFORM_AFTER [3000-4000]
您可以管理拦截器的主要负载。