Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI - what is the correct bean.xml format?

I have a question about the correct format and usage of the bean.xml file. In my projects I typically used this content for my bean.xml files (no explizit bean declaration used):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

This works well in WildFly 8 and 9. But I have deployment issues in GlassFish 4. In the question: Glassfish 4, simple example in CDI fails with WELD-001408 Unsatisfied dependencies I wrote about an alternative format:

<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>

There a different namespaces used. And GlassFish4 seems to care about that.

What is the correct format of an empty bean.xml File used for JEE7 ?

like image 825
Ralph Avatar asked Oct 05 '15 06:10

Ralph


People also ask

What is a Beans xml?

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.

What is CDI bean?

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.

Where do I put beans xml?

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

What is a bean archive?

A bean archive is any module that contains beans that the CDI runtime can manage and inject. There are two kinds of bean archives: explicit bean archives and implicit bean archives.


1 Answers

Correct empty beans.xml can be totally empty file, really ;-)

But when you want to add some content, please notice that most of the XML deployment descriptor namespaces have been updated in Java EE 7. This post describes the details. Also bean-discovery-mode has been added.

BTW: Sample beans.xml which I'm using right now looks like:

<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"
       version="1.2" bean-discovery-mode="annotated">

    <!-- some content -->
</beans>

You may notice the usage of version="1.2" attribute - you can freely set it to 1.1. It just serves as a reminder to the reader that project is using CDI 1.2 (which in fact is just a Maintenance release of the CDI 1.1 Specification).

like image 199
G. Demecki Avatar answered Sep 29 '22 14:09

G. Demecki