-
1. Re: injection into a programmatically registered servlet
asiandub Aug 31, 2010 3:05 PM (in response to marcelkolsteren)
Marcel, can you please ask this in the Weld user forum here http://seamframework.org/Community/WeldUsersForum? Weld developers hang out there.I'm certainly not one of those, so consider it my two cents:
I'm wondering when (and by whom) this class is instantiated:
package nl.meandi.examples; import java.io.IOException; import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { private static final long serialVersionUID = -2831626634018329617L; @Inject RequestScopedBean requestScopedBean; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (requestScopedBean == null) { resp.setContentType("text/plain"); resp.getWriter().print("NOT_OK"); resp.flushBuffer(); } else { resp.setContentType("text/plain"); resp.getWriter().print("OK"); resp.flushBuffer(); } } }
The question I'm trying to raise: Is TestServlet a managed bean / contextual component in both cases?
Looking forward to learn about the answer,
Jan
-
2. Re: injection into a programmatically registered servlet
marcelkolsteren Sep 1, 2010 4:49 AM (in response to marcelkolsteren)Hi Jan,
The TestServlet is instantiated twice:
- In web.xml, a servlet named
TestServlet1
is defined, referring to the class TestServlet. - In the ServletInstaller class, which listens to the servlet context initialization event, the TestServlet is instantiated programmatically, by calling addServlet on the servlet context. This results in a servlet named
TestServlet2
.
In both cases, TestServlet is not a managed bean, it's not contextual, and it doesn't have a scope. It is an instance one of a Java EE component types for which dependency injection can be used. Quote from the CDI spec, section 5.5:
The container is also required to perform dependency injection whenever it instantiates any of the following non-contextual objects:
non-contextual instances of session beans (...), non-contextual instances of managed beans, and instances of any other Java EE component class supporting injection.Servlets are an example of the last mentioned category of injection-capable components. I hope this cleared things up for you.
Marcel
- In web.xml, a servlet named
-
3. Re: injection into a programmatically registered servlet
marcelkolsteren Sep 3, 2010 2:37 PM (in response to marcelkolsteren)I talked with Pete Muir and Nicklas Karlsson about this issue. Because the spec doesn't seem to contain a hard requirement concerning this subject, I submitted a feature request (instead of a bug report):
JBAS-8387
According to Pete, it's certainly possible to support this feature.