-
Bug
-
Resolution: Done
-
Major
-
5.0.0.SP1
-
None
-
None
When using Listener.using(WeldContainer) to pass a previously created Weld SE container to a Servlet in order to have injection available the startup of the servlet fails with a NullPointerException:
Juni 05, 2022 10:55:10 VORM. org.jboss.weld.bootstrap.WeldStartup <clinit> INFO: WELD-000900: 5.0.0 (SP1) Juni 05, 2022 10:55:10 VORM. org.jboss.weld.environment.deployment.discovery.ReflectionDiscoveryStrategy processAnnotatedDiscovery INFO: WELD-ENV-000014: Falling back to Java Reflection for bean-discovery-mode="annotated" discovery. Add org.jboss:jandex to the classpath to speed-up startup. Juni 05, 2022 10:55:10 VORM. org.jboss.weld.bootstrap.WeldStartup startContainer INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. Juni 05, 2022 10:55:11 VORM. org.jboss.weld.environment.se.WeldContainer fireContainerInitializedEvent INFO: WELD-ENV-002003: Weld SE container 5fc38f81-aef2-49f9-b2ed-34473ea401bb initialized Juni 05, 2022 10:55:11 VORM. org.eclipse.jetty.server.Server doStart INFORMATION: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.11+9 Juni 05, 2022 10:55:11 VORM. org.jboss.weld.environment.servlet.Listener contextInitialized INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener Juni 05, 2022 10:55:11 VORM. org.jboss.weld.environment.jetty.JettyLegacyContainer initialize INFO: WELD-ENV-001201: Jetty 7.2+ detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported. Exception in thread "main" java.lang.NullPointerException at org.jboss.weld.module.web.servlet.HttpContextLifecycle.contextInitialized(HttpContextLifecycle.java:154) at org.jboss.weld.module.web.servlet.WeldInitialListener.contextInitialized(WeldInitialListener.java:99) at org.jboss.weld.servlet.api.helpers.ForwardingServletListener.contextInitialized(ForwardingServletListener.java:34) at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:121) at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:1040) at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:624) at org.eclipse.jetty.server.handler.ContextHandler.contextInitialized(ContextHandler.java:977) at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:705) at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:392) at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:896) at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:306) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:171) at org.eclipse.jetty.server.Server.start(Server.java:469) at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114) at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:89) at org.eclipse.jetty.server.Server.doStart(Server.java:414) at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:93) at weldjetty.Main.main(Main.java:36) Weld SE container 5fc38f81-aef2-49f9-b2ed-34473ea401bb shut down by shutdown hook
This used to work flawlessly in Weld <5.
It also works if the listener does creation of the weld container, however this is not helpful for me.
Example:
public class Main { public static void main(String[] args) throws Exception { Weld weld = new Weld(); WeldContainer weldContainer = weld.initialize(); Server jetty = new Server(7777); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); context.setResourceBase("src/main/resources"); jetty.setHandler(context); context.addServlet(HelloWorldServlet.class, "/*"); context.addEventListener(Listener.using(weldContainer)); jetty.start(); jetty.join(); } @ApplicationScoped public static class HelloWorldServlet extends HttpServlet { @Inject BeanManager manager; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.getWriter().append("Hello from " + manager); } } }