8

I am using weld,a RI of CDI as dependency injection component in my JSF-EJB-JPA web app. I see in my project we have empty beans.xml in META-INF/beans.xml in ejb.jar and WEB-INF/beans.xml in my WAR. I don't get it why we need to keep empty beans.xml when there nothing defined in that file?

SRy
  • 2,901
  • 8
  • 36
  • 57
  • It's actually too bad you can't accept more than one answer, because both rdcrng and Flo are correct. – LightGuard Aug 08 '13 at 23:16
  • @LightGuard... I am about to accept the answer by `rdcrng`. Meanwhile you posted this comment – SRy Aug 09 '13 at 01:10

3 Answers3

8

CDI needs to scan all the classes of a bean archive at start-up and fire a bunch of events because almost any class is automatically a managed bean (read more here), even if it doesn't have any annotations.

This would incur quite a bit of overhead, especially for jar files that are not meant to have any beans. It is therefore beneficial to explicitly indicate which bean archives should be scanned by including the beans.xml, even an empty beans.xml.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
rdcrng
  • 3,415
  • 2
  • 18
  • 36
  • 2
    `it is therefore beneficial to explicitly indicate which bean archives should be scanned`, I am confused at this point. When we are including a empty `beans.xml`, how does container know which archives it has scan to create instances to injection purposes? – SRy Aug 08 '13 at 16:54
  • 6
    the mere existence of beans.xml is indicator the archive should be scanned – Aksel Willgert Aug 08 '13 at 17:40
  • @AkselWillgert Thanks :) @SRy Just like Aksel Willgert said, the container looks specifically for the presence of `beans.xml` first and based on it decides whether to scan further in that archive. – rdcrng Aug 09 '13 at 11:32
  • @rdcrng.... Yes after your answer and `Askel's` comment this article make sense to me. http://www.seamframework.org/Documentation/WhatIsBeansxmlAndWhyDoINeedIt – SRy Aug 09 '13 at 13:52
5

1

A completely empty beans.xml is the same as having a beans.xml inside the archive with the following content:

<?xml version="1.0" encoding="UTF-8"?>

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

Because of bean-discovery-mode="all" the archive will be scanned for beans. No need to annotate them.

2

A non-existent beans.xml it is the same as having a beans.xml inside the archive with the following content:

<?xml version="1.0" encoding="UTF-8"?>

<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="annotated">

</beans>

Because of bean-discovery-mode="annotated" the archive will be scanned for beans among classes that are annotated (e.g. @Dependent ). All other classes will be ignored, therefore cannot be injected as beans.

3

A third option is to declare bean-discovery-mode="none" in which case the server never scans the archive for beans.

4

Now for the case on which you want to load a class as a bean but you cannot access the archive (e.g. external library) and the class is not annotated, the solution is to use a Producer methods (with or without qualifiers).

Marinos An
  • 9,481
  • 6
  • 63
  • 96
0

It is used in certain limited situations

http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html

Some CDI features like decorators would be declared in this file

Flo
  • 411
  • 4
  • 11