Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Weld as CDI implementation. 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. 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!

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. 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 :/

like image 430
Xorty Avatar asked Oct 24 '12 19:10

Xorty


People also ask

Where should I put beans xml?

The bean archive descriptor beans. xml should be located at META-INF/beans.

What is Beans xml for?

The beans. xml file is the bean archive descriptor for CDI applications. It can be used for any CDI compliant container, such as Weld which is included in WildFly application server.

Do I need beans xml?

For version 1.0 of CDI , beans. xml is mandatory to enable CDI bean discovery. Without beans. xml , CDI is simply not active in the corresponding archive.

What are CDI beans?

A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.


2 Answers

For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/.

For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/.

Remember that only .java files should be put in the src/main/java and src/test/java directories. Resources like .xml files should be in src/main/resources.

like image 67
maba Avatar answered Sep 20 '22 23:09

maba


Just to complement the above answer, here is an official reference on this: https://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

quote:

An application that uses CDI must have a file named 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. For EJB modules or JAR files, the beans.xml file must be in the META-INF directory.

like image 34
99Sono Avatar answered Sep 21 '22 23:09

99Sono