我使用 Weld 作为 CDI 实现。当我在 src/test/java/META-INF/beans.xml
中有空 beans.xml 时,我的集成测试尝试组装对象图实例化 Weld 容器 运行良好>。这是一个简单的测试:
public class WeldIntegrationTest {
@Test
public void testInjector() {
new Weld().initialize();
// shouldn't throw exception
}
}
现在当我运行 mvn clean install
时,我总是得到:META-INF 中缺少 beans.xml 文件!
我的根文件夹是“src”和“web”,其中包含 WEB-INF 文件夹,但我也尝试使用默认的 maven 结构并将“web”重命名为“webapp”并将其移动到 src/main。我尝试了所有我能想到的合理位置:
- src/main/java/META-INF/beans.xml
- src/test/java/META-INF/beans.xml
- web/WEB-INF/beans.xml
- src/main/webapp/WEB-INF/beans.xml
- src/main/webapp/META-INF/beans.xml
- src/main/webapp/META-INF/(empty) and src/main/webapp/WEB-INF/beans.xml
到目前为止没有任何效果:/
最佳答案
对于 EJB 和 JAR 打包,您应该将 beans.xml
放在 src/main/resources/META-INF/
中。
对于 WAR 打包,您应该将 beans.xml
放在 src/main/webapp/WEB-INF/
中。
请记住,只有 .java
文件应该放在 src/main/java
和 src/test/java
目录中。 .xml
文件之类的资源应该在 src/main/resources
中。
关于java - CDI:beans.xml,我把你放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13056336/