将 CDI 拦截器注入到 Bean 中不起作用

发布于 2025-01-11 18:57:46 字数 1394 浏览 6 评论 0原文

我创建了一个 CDI (WELD) 拦截器,它可以工作并拦截它应该拦截的内容。

@MyInterceptorBinding
@Interceptor
@Dependent
public class MyInterceptor implements Serializable {

    private int myIntegerField;
    
    @AroundInvoke
    public Object interceptMethod(InvocationContext ctx) throws Exception {

        // Do some operations and side effects on myIntegerField;

        try {
            Object result = ctx.proceed();
            return result;
        } catch (Exception e) {
            throw e;
        }
    }

    public List<Class<?>> getMyIntegerField() {
        return myIntegerField;
    }

}

其中 MyInterceptorBinding 是拦截器绑定:

@Inherited
@InterceptorBinding
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyInterceptorBinding {}

我想将拦截器注入到这样的 bean 类中:

@ApplicationScoped
public class MyBean implements Serializable {

    @Inject
    private MyInterceptor interceptor;

    public void aMethod(){
        int var = interceptor.getMyIntegerField();
        // use var in some way...
   }
}

但这种注入会带来错误:

Unsatisfied dependencies for type MyInterceptor with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private trials.MyBean.interceptor

我怎样才能克服这个问题?问题是否与拦截器有关? 我应该使用 CDI 便携式扩展设备吗?如果是这样,怎么办?

I have created a CDI (WELD) interceptor that works and intercept what it is supposed to intercept.

@MyInterceptorBinding
@Interceptor
@Dependent
public class MyInterceptor implements Serializable {

    private int myIntegerField;
    
    @AroundInvoke
    public Object interceptMethod(InvocationContext ctx) throws Exception {

        // Do some operations and side effects on myIntegerField;

        try {
            Object result = ctx.proceed();
            return result;
        } catch (Exception e) {
            throw e;
        }
    }

    public List<Class<?>> getMyIntegerField() {
        return myIntegerField;
    }

}

Where MyInterceptorBinding is an interceptor binding:

@Inherited
@InterceptorBinding
@Target({TYPE, METHOD, PARAMETER, FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyInterceptorBinding {}

I would like to inject my interceptor into a bean class like this:

@ApplicationScoped
public class MyBean implements Serializable {

    @Inject
    private MyInterceptor interceptor;

    public void aMethod(){
        int var = interceptor.getMyIntegerField();
        // use var in some way...
   }
}

but this injection brings to an error:

Unsatisfied dependencies for type MyInterceptor with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private trials.MyBean.interceptor

How can i overcome this issue? Is a problem related to the fact that is an interceptor?
Should i use the CDI portable extension facility? And, if so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2025-01-18 18:57:46

你不能注入拦截器,因为它们不是 bean。您可能可以做的是创建一个辅助 bean 并将其注入拦截器和应用程序范围 bean 中。然后,该辅助 bean 可以包含该值,拦截器可以设置它,而另一个 bean 可以读取它。

关于范围的警告:您的 MyBean 是应用程序范围的,这意味着只会有一个实例。如果拦截器用在请求作用域的 bean 中,那么拦截器值应该是多少?为此,辅助 bean 很可能也应该是应用程序范围的,并且您应该注意线程安全。

You can't inject interceptors because they aren't beans. What you probably can do is create a helper bean and inject that in both the interceptor and the application scoped bean. That helper bean can then contain the value, the interceptor can set it and the other bean can read it.

A warning about scopes: your MyBean is application scoped, which means that there will be only once instance. If the interceptor is used in request scoped beans, what should the value of the interceptor value be? For this to work, the helper bean should most likely also be application scoped, and you should take care of thread safety.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文