Implicit bean archive
CDI 1.1 introduced the notation of implicit bean archives, making the inclusion of beans.xml optional. Only beans annotated with bean defining annotations will be considered.
An implicit bean archive is any other archive which contains one or more bean classes with a bean defining annotation, or one or more session beans.
CDI 1.1 specification, 12.1. Bean archives
It is important to note that CDI 1.1 defined all JSR-330 javax.inject.* annotations as bean defining, however this caused some problems with archives that use other JSR-330 compliant injection frameworks, most notably google guava library.
The CDI 1.2 maintenance release addresses these by limiting bean defining annotations to:
- @ApplicationScoped, @SessionScoped, @ConversationScoped and @RequestScoped annotations,
- all other normal scope types,
- @Interceptor and @Decorator annotations,
- all stereotype annotations (i.e. annotations annotated with @Stereotype),
- and the @Dependent scope annotation.
Any bean that does not define at least one bean defining annotation, will be ignored by CDI. Most notably beans solely defining @Named or CDI pseudo-scopes other than @Dependent, such as @Singleton will be ignored.
Explicit bean archive
A bean archive which contains a beans.xml file with no version has a default bean discovery mode of all.
A bean archive which contains a beans.xml file with version 1.1 (or later) must specify the bean-discovery-mode attribute. The default value for the attribute is annotated.
…
An explicit bean archive is an archive which contains a beans.xml file:
- with a version number of 1.1 (or later), with the bean-discovery-mode of all, or,
- with no version number, or,
- that is an empty file.
Bean archives with a beans.xml set to version 1.1 or later are required to define the bean-discovery-mode (beans_1_1.xsd defines the default version as 1.1). For backwards compatibility, bean-archives that don’t specify a version will be treated with bean discovery mode “all”. This includes empty beans.xml as well as CDI 1.0 beans.xml defining the beans_1_0.xsd schema, as the version attribute was added with CDI 1.1.
Conclusion
The annotated bean discovery mode, which was introduced with CDI 1.1 to Java EE 7, has many pitfalls requiring developers to rethink the way they declare beans. Alternatively developers may prefer to resort to the known and trusted bean discovery mode “all”, mimicking CDI 1.0 behavior.
Only time will tell if implicit bean declarations and the annotated bean discovery mode will get accepted by the Java EE community.
Warning
At the time of writing there is a bug in Wildfly 8.2.1.FINAL, affecting Vaadin-CDI war projects that are packaged into an ear.When deploying the war directly, the correct bean discovery mode is applied to the vaadin-cdi bean-archive, in this case “all” since no version is specified in the vaadin-cdi beans.xml.
However when the same war is deployed as part of an ear file, CDI does not seem to find the Vaadin CDI beans, my best guess is that the bean discovery mode of the war is applied to the vaadin-cdi bean archive.To work around this issue you will have to add beans.xml to your war project with the beans discovery mode set to “all”.