CDI注入点@httpparam问题

发布于 2024-09-17 07:11:40 字数 812 浏览 5 评论 0原文

我遵循 Weld 的文档 第 4.11 节。 InjectionPoint 对象

有一个非常有趣的示例,介绍如何使用 CDI 获取 http 参数,

但我将代码复制粘贴到 netbeans 中,所有内容都可以编译,但出现部署错误

原因为:org.jboss.weld.exceptions.DeploymentException:WELD -001408 注入点具有不满足的依赖性。注入点:java.lang.String com.test.HttpParamProducer.getParamValue(javax.enterprise.inject.spi.InjectionPoint,javax.servlet.ServletRequest)的参数1;限定符:[@javax.enterprise.inject.Default()]

如何解决这个问题???

public class HttpParamProducer {

   @HttpParam("")
   @Produces
   String getParamValue(
           InjectionPoint ip, ServletRequest request) {

      return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value());

   }
}

I follow the Weld's doc

in the section 4.11. The InjectionPoint object

There is a very interesting example about how to obtain the http parameter using CDI

but i copy-pasted the code into netbeans, everything compiles, but has an deployment error

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Injection point has unsatisfied dependencies. Injection point: parameter 1 of java.lang.String com.test.HttpParamProducer.getParamValue(javax.enterprise.inject.spi.InjectionPoint,javax.servlet.ServletRequest); Qualifiers: [@javax.enterprise.inject.Default()]

how to solve this problem???

public class HttpParamProducer {

   @HttpParam("")
   @Produces
   String getParamValue(
           InjectionPoint ip, ServletRequest request) {

      return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value());

   }
}

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

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

发布评论

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

评论(2

神仙妹妹 2024-09-24 07:11:40

生产者方法上的每个参数都会被注入,并且您的任何 Bean(包括生产者)都不会提供 API 类型 ServletRequest 来满足此注入点。

Every parameter on a producer method is injected, and none of your beans (including producers) provides the API type ServletRequest to satisfy this injection point.

溺渁∝ 2024-09-24 07:11:40

似乎两年后,这个问题仍然令人感兴趣

,这是 CDI 规范的一个缺点,它不需要容器将 HttpServletRequest 作为可注入 bean 公开,

这里是一个合理的解决方法

@WebListener 
public class HttpServletRequestProducer implements ServletRequestListener {
    private final static ThreadLocal<HttpServletRequest> holder = new ThreadLocal<HttpServletRequest>();

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        holder.remove();
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        holder.set((HttpServletRequest)sre.getServletRequest());
    }

    @Produces @RequestScoped HttpServletRequest get() {
        return holder.get();
    }
}

,现在 @Inject HttpServletRequest 将工作为期待

快乐的编码

it seems that after two years, this question is still interested

this is a short coming of the CDI spec, where it doesn't require the container to expose HttpServletRequest as injectable bean

here is a reasonable work around

@WebListener 
public class HttpServletRequestProducer implements ServletRequestListener {
    private final static ThreadLocal<HttpServletRequest> holder = new ThreadLocal<HttpServletRequest>();

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        holder.remove();
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        holder.set((HttpServletRequest)sre.getServletRequest());
    }

    @Produces @RequestScoped HttpServletRequest get() {
        return holder.get();
    }
}

now @Inject HttpServletRequest will be working as expected

happy coding

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