在IntelliJ中使用Weld SE和CDI时容器初始化失败求助
Weld SE + CDI 项目启动错误解决方案
问题场景
在IntelliJ Ultimate版中创建基于Weld SE和CDI的基础项目:选择Jakarta EE Web Application模板,采用Maven构建,添加CDI与Weld SE依赖,使用JDK 17。
项目代码
public interface Pet { } public class Dog implements Pet { } public class Person { Pet itsPet; @Inject public Person(Pet pet){ itsPet = pet; } } import org.jboss.weld.environment.se.Weld; import org.jboss.weld.environment.se.WeldContainer; public class MainApp { public static void main(String[] args) { WeldContainer container = new Weld().initialize(); Person person = container.select(Person.class).get(); } }
启动错误
Mar 17, 2023 8:21:41 AM org.jboss.weld.bootstrap.WeldStartup <clinit> INFO: WELD-000900: 4.0.3 (Final) Mar 17, 2023 8:21:41 AM 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. Exception in thread "main" java.lang.IllegalStateException: WELD-ENV-002009: Weld SE container cannot be initialized - no bean archives found at org.jboss.weld.environment.se.Weld.createDeployment(Weld.java:995) at org.jboss.weld.environment.se.Weld.initialize(Weld.java:787) at student.MainApp.main(MainApp.java:8)
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>try</artifactId> <version>1.0-SNAPSHOT</version> <name>try</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.source>11</maven.compiler.source> <junit.version>5.9.1</junit.version> </properties> <dependencies> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> <artifactId>weld-se-core</artifactId> <version>4.0.3.Final</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> </plugin> </plugins> </build> </project>
beans.xml位置与内容
位于target/classes/META-INF/下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd" bean-discovery-mode="annotated"> </beans>
解决方案
1. 修正Maven打包格式
Weld SE是Java SE环境,当前war打包方式适配Web应用,改为jar:
<packaging>jar</packaging>
同时替换maven-war-plugin为maven-jar-plugin(可选,更贴合SE环境):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>student.MainApp</mainClass> <!-- 指定主类 --> </manifest> </archive> </configuration> </plugin>
2. 统一JDK版本配置
pom中source/target为11,实际使用JDK17,同步版本:
<properties> <!-- 其他属性保留 --> <maven.compiler.target>17</maven.compiler.target> <maven.compiler.source>17</maven.compiler.source> </properties>
3. 调整Bean发现策略
当前bean-discovery-mode="annotated"要求类必须带有CDI注解才会被识别为bean,有两种解决方式:
方式一:给类添加CDI注解
给Dog和Person添加@Named注解(或其他CDI作用域注解如@ApplicationScoped):
import jakarta.inject.Named; @Named public class Dog implements Pet { } @Named public class Person { Pet itsPet; @Inject public Person(Pet pet){ itsPet = pet; } }
方式二:修改beans.xml的发现模式
将bean-discovery-mode改为all,扫描所有类作为候选bean:
<bean-discovery-mode="all">
4. 确保beans.xml在源码目录
将beans.xml从target/classes/META-INF/复制到src/main/resources/META-INF/,这样Maven编译时会自动将其复制到target目录,保证运行时能被Weld识别。
内容的提问来源于stack exchange,提问作者Shaharg
相关产品推荐
相关产品推荐

