Interceptor method not called with interceptor binding

旧巷老猫 提交于 2019-12-18 04:38:08

问题


I'm using Java EE 6 & Jboss AS7.1 and try to use interceptor binding (Example from jboss site).

I have an InterceptorBinding annotation:

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

The interceptor:

@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();
    }
}

And a bean:

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

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

But the interceptor is not called... What do I miss from the example ?

The interceptor is called when I write this:

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

Thanks for your help.


回答1:


Did you enable your interceptor as described in the referenced example?

By default, a bean archive has no enabled interceptors bound via interceptor bindings. An interceptor must be explicitly enabled by listing its class under the element of the beans.xml file of the bean archive.




回答2:


According to the documentation there is another way rather than using beans.xml:

You do not need to specify the interceptor in the beans.xml file when you use the @Priority annotation.

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

And it works.




回答3:


You can use any priority value = Priority.Application is 2000 by default.

For example =

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

Priority type:

  • PLATFORM_BEFORE [0-999] - interceptors start at the first. They are started by the platform.
  • LIBRARY_BEFORE [1000-1999] - by the libraries.
  • APPLICATION [2000-2999]- by the application
  • LIBRARY_AFTER,PLATFORM_AFTER [3000-4000]

You manage primary loading for interceptors.



来源:https://stackoverflow.com/questions/12076586/interceptor-method-not-called-with-interceptor-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!