Servlet 是 Java EE 中的 CDI/Managed Bean

Is Servlet a CDI/Managed Bean in Java EE

在容器环境中(如wildfly,jboss),servlets是否被视为Managed bean?即我可以将 Servlet 注入任何其他 CDI bean 吗?

我使用 CdiRunner CDI-Unit 编写我的测试。因此我想将 Servlet 注入我的测试 class 并测试它的 (public) 方法。

如果由 servlet 容器而非 CDI 管理,则 servlet 的生命周期。然而,CDI 注入有望在 servlet 中发挥作用。

一个servlet容器也会提供一些可以使用CDI注入的built-in beans:

A servlet container must provide the following built-in beans, all of which have qualifier @Default:

  • a bean with bean type javax.servlet.http.HttpServletRequest, allowing injection of a reference to the HttpServletRequest

  • a bean with bean type javax.servlet.http.HttpSession, allowing injection of a reference to the HttpSession

  • a bean with bean type javax.servlet.ServletContext, allowing injection of a reference to the ServletContext

如果你需要在某处注入一个 servlet,你可能做错了什么。