Reputation: 191
In a container environment (such as wildfly, jboss), are servlets treated as Managed bean? i.e. Can I inject the Servlet into any other CDI bean?
I use CdiRunner CDI-Unit to write my tests. And therefore I would like to inject Servlet into my Test class and test its (public) methods.
Upvotes: 2
Views: 614
Reputation: 130857
The lifecycle of a servlet if managed by the servlet container and not by CDI. However, CDI injection is expected to work in servlets.
A servlet container will also provide some built-in beans that can be injected using CDI:
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 theHttpServletRequest
a bean with bean type
javax.servlet.http.HttpSession
, allowing injection of a reference to theHttpSession
a bean with bean type
javax.servlet.ServletContext
, allowing injection of a reference to theServletContext
If you need to inject a servlet somewhere, you are probably doing something wrong.
Upvotes: 2