2012-08-22 189 views
10

我正在使用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) { 
    /* ... */ 
} 

感谢您的帮助。

回答

12

您是否按照引用的示例中所述启用了拦截器?

默认情况下,一个bean档案已通过 拦截器绑定约束没有启用的拦截器。拦截器必须由 明确启用,其中的类将其类列出在bean归档文件的beans.xml 文件的元素下。

+1

非常感谢,我太快地阅读了这个例子,我认为使用注释并不是必须使用beans.xml,但事实上它是...... –

+2

这似乎也是JavaEE7的情况。 –

11

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

你并不需要指定的beans.xml的文件拦截时 使用@priority注解。

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

和它的作品。

+1

非常感谢!你解决了我的问题:) –

+0

非常感谢你!你也解决了我的问题:) – KeyMaker00

3

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

您管理的拦截主加载。