Jetty12+Java21+Wicket环境下Weld无法识别@Inject注入问题
Jetty+Wicket+Java 21环境下Weld依赖注入失效问题排查与解决
尝试搭建基于Jetty、Apache Wicket和Java 21的小型应用,发现Weld依赖注入无法识别@Inject和@RequestScoped注解,所有标注@Inject的类在运行时始终为null(这些类已添加@Named和@RequestScoped注解)。环境基于wicket.apache.org的默认Wicket Maven快速启动项目修改,已适配Java 21与EE10。
pom.xml依赖版本
// [...] pom.xml 其他内容 <properties> <wicket.version>10.1.0</wicket.version> <slf4j.version>2.0.13</slf4j.version> <jetty.version>12.0.7</jetty.version> <junit.version>5.10.2</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- allowed values: R7, 1.0, 1.5, 2.0 or none --> <wtp.version>none</wtp.version> </properties> <dependencies> <!-- WICKET DEPENDENCIES --> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-core</artifactId> <version>${wicket.version}</version> </dependency> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-native-websocket-javax</artifactId> <version>${wicket.version}</version> </dependency> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-cdi</artifactId> <version>${wicket.version}</version> </dependency> <dependency> <groupId>jakarta.enterprise</groupId> <artifactId>jakarta.enterprise.cdi-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-core</artifactId> <version>5.1.3.Final</version> </dependency> <dependency> <groupId>org.eclipse.jetty.ee10.websocket</groupId> <artifactId>jetty-ee10-websocket-jakarta-server</artifactId> <version>12.0.13</version> </dependency> <dependency> <groupId>org.eclipse.jetty.ee10</groupId> <artifactId>jetty-ee10-servlet</artifactId> <version>12.0.13</version> <scope>test</scope> </dependency> // [...] pom.xml 其他内容
Start.java启动代码
public static void main(String[] args) throws Exception { System.setProperty("wicket.configuration", "development"); Server server = new Server(); HttpConfiguration httpConfig = new HttpConfiguration(); httpConfig.setSecureScheme("https"); httpConfig.setSecurePort(8443); httpConfig.setOutputBufferSize(32768); ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(httpConfig)); http.setPort(8080); http.setIdleTimeout(1000 * 60 * 60); server.addConnector(http); WebAppContext bb = new WebAppContext(); bb.setServer(server); bb.setContextPath("/"); bb.setWar("src/main/webapp"); bb.addEventListener(new Listener()); // 预期用于Wicket-CDI注入 ServletContextHandler contextHandler = ServletContextHandler.getServletContextHandler(bb.getServletContext()); JakartaWebSocketServletContainerInitializer.configure(contextHandler, (servletContext, container) -> container.addEndpoint(new WicketServerEndpointConfig())); server.setHandler(bb); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer); server.addEventListener(mBeanContainer); server.addBean(mBeanContainer); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); System.exit(100); } }
运行时错误日志
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. org.jboss.weld.bootstrap.WeldStartup startContainer Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. org.jboss.weld.environment.servlet.WeldServletLifecycle initialize INFO: WELD-ENV-001001: No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners [INFO] Session workerName=node0 [INFO] Started oeje10mp.MavenWebAppContext@1f10fec6{tractorStore,/,b=file://my-app/src/main/webapp/,a=AVAILABLE,h=oeje10s.SessionHandler@144e36ae{STARTED}}{file://my-app/src/main/webapp/} org.jboss.weld.environment.servlet.Listener contextInitialized INFO: WELD-ENV-001006: org.jboss.weld.environment.servlet.EnhancedListener used to initialize Weld org.jboss.weld.environment.servlet.EnhancedListener contextInitialized INFO: WELD-ENV-001009: org.jboss.weld.environment.servlet.Listener used for ServletRequest and HttpSession notifications [main] INFO org.apache.wicket.util.file.WebXmlFile - web.xml: url mapping found for filter with name wicket.tractorStore: [/*] [main] INFO org.apache.wicket.Application - [wicket.tractorStore] init: Wicket core library initializer [main] INFO org.apache.wicket.protocol.http.WebApplication - [wicket.tractorStore] Started Wicket version 10.1.0 in DEVELOPMENT mode
关键问题日志:
INFO: WELD-ENV-001001: No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners
解决方案
- 在
WEB-INF目录下添加beans.xml文件:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all"> </beans>
- 在
WicketApplication.java中添加CDI配置代码:
CdiConfiguration cdiConfiguration = new CdiConfiguration(); cdiConfiguration.configure(this);
内容的提问来源于stack exchange,提问作者aukevanoost
相关产品推荐
相关产品推荐

