2

I am coming at this question from many years of using spring and just starting to look at JEE7 and CDI.

In the Spring world you have to stick @Component on a bean to turn into spring bean that spring will inject with dependencies but in CDI it seems that there is no equivalent of @Component.

To me CDI seems to imply that every class in my web application will be considered a CDI bean which seems undesirable because I have lot of java classes that are not using injection and I would not want some one to just stick @Inject in those classes and have CDI do its magic.

Two questions:

  • How to restrict what CDI considers to be a managed bean in a jar file?
  • What is the benefit for CDI to consider every bean to be a managed bean?
ams
  • 60,316
  • 68
  • 200
  • 288

1 Answers1

1

Please see the documentation for bean-discovery-mode in beans.xml. This attribute was only made available in JEE7 and is not available in JEE6.

Sekhon
  • 108
  • 1
  • 10
  • can you elaborate on the bean discovery mode and the mechanism used by cdi. – ams Nov 18 '13 at 06:05
  • There are 3 possible values. ALL, NONE, ANNOTATED. ALL means all beans that are can be a CDI bean (See http://docs.oracle.com/javaee/6/tutorial/doc/gjfzi.html) are managed by the container in the archive. NONE means no Class file in the JAR is managed by the CDI .container. While ANNOTATED tmakes the archive an implicit archive. In this case, the container will scan for beans with annotated scope types. Also see answer http://stackoverflow.com/questions/18310388/meaning-of-bean-discovery-mode-annotated-in-cdi-1-1 – Sekhon Nov 18 '13 at 06:23
  • Also any bean that you want not applicable can be given the annotation @Vetoed and CDI container will ignore it. See http://docs.oracle.com/javaee/7/tutorial/doc/cdi-adv001.htm – Sekhon Nov 18 '13 at 06:28