2012-08-22 186 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]

您管理的攔截主加載。