简体   繁体   English

CDI:beans.xml,我在哪里放你?

[英]CDI: beans.xml, where do I put you?

I am using Weld as CDI implementation. 我使用Weld作为CDI实现。 My integration test, that tries to assemble object graph instantiating Weld container works well , when I have empty beans.xml in src/test/java/META-INF/beans.xml . 当我在src/test/java/META-INF/beans.xml有空beans.xml时,我试图组装对象图实例化Weld容器的集成测试运行良好 Here is that simple test: 这是一个简单的测试:

public class WeldIntegrationTest {
    @Test
    public void testInjector() {
        new Weld().initialize();
        // shouldn't throw exception
    }
}

Now when I run mvn clean install , I always get: Missing beans.xml file in META-INF! 现在当我运行mvn clean install ,我总是得到: Missing beans.xml file in META-INF!

My root folders are "src" and "web" which contains WEB-INF folder, but I also tried to use default maven structure and renamed "web" to "webapp" and moved it to src/main. 我的根文件夹是“src”和“web”,其中包含WEB-INF文件夹,但我也尝试使用默认的maven结构并将“web”重命名为“webapp”并将其移至src / main。 I tried all the reasonable locations I could thought of: 我尝试了所有合理的位置:

 - 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

Nothing works so far :/ 到目前为止没有任何作用:/

For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/ . 对于EJB和JAR打包,您应该将beans.xml放在src/main/resources/META-INF/

For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/ . 对于WAR打包,您应该将beans.xml放在src/main/webapp/WEB-INF/

Remember that only .java files should be put in the src/main/java and src/test/java directories. 请记住,只有.java文件应放在src/main/javasrc/test/java目录中。 Resources like .xml files should be in src/main/resources . .xml文件这样的资源应该在src/main/resources

Just to complement the above answer, here is an official reference on this: https://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html 为了补充上述答案,以下是对此的官方参考: https//docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

quote: 引用:

An application that uses CDI must have a file named beans.xml. 使用CDI的应用程序必须具有名为beans.xml的文件。 The file can be completely empty (it has content only in certain limited situations), but it must be present. 该文件可以完全为空(它仅在某些有限的情况下具有内容),但必须存在。 For a web application, the beans.xml file must be in the WEB-INF directory. 对于Web应用程序,beans.xml文件必须位于WEB-INF目录中。 For EJB modules or JAR files, the beans.xml file must be in the META-INF directory. 对于EJB模块或JAR文件,beans.xml文件必须位于META-INF目录中。

http://www.javamonamour.org/2017/11/cdi-in-java-se-8.html here you can see where I have put it with success, that is under src/META-INF . http://www.javamonamour.org/2017/11/cdi-in-java-se-8.html在这里你可以看到我把它成功放在哪里,就是在src / META-INF下。 The post contains a full working example of CDI in Java SE. 该帖子包含Java SE中CDI的完整工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM