java 未使用拦截器绑定调用拦截器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12076586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:30:23  来源:igfitidea点击:

Interceptor method not called with interceptor binding

javaejbjava-ee-6interceptor

提问by Philippe Gonday

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

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

I have an InterceptorBinding annotation:

我有一个 InterceptorBinding 注释:

@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.

谢谢你的帮助。

采纳答案by zellus

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.

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

回答by andrew.dman

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

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

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

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

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

And it works.

它有效。

回答by Boris Zhguchev

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

您可以使用任何优先级值 = Priority.Application 默认为 2000。

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]
  • PLATFORM_BEFORE [0-999] - 拦截器从第一个开始。它们由平台启动。
  • LIBRARY_BEFORE [1000-1999] - 由图书馆。
  • 申请[2000-2999]-由申请
  • LIBRARY_AFTER,PLATFORM_AFTER [3000-4000]

You manage primary loading for interceptors.

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