2

What does it mean when Weld says "unsatisfied dependency for type Foo" when the bean is POJO but, as soon as I add @Dependent at the top, everything works ok? I remember it used to work fine without specifying the scope. I think I broke something.

The specs say:

A managed bean is implemented by a Java class, which is called its bean class. A top-level Java class is a managed bean if it is defined to be a managed bean by any other Java EE technology specification, such as the JavaServer Faces technology specification, or if it meets all the following conditions.

  • It is not a nonstatic inner class.

  • It is a concrete class or is annotated @Decorator.

  • It is not annotated with an EJB component-defining annotation or declared as an EJB bean class in ejb-jar.xml.

  • It has an appropriate constructor. That is, one of the following is the case.

  • The class has a constructor with no parameters.

  • The class declares a constructor annotated @Inject.

No special declaration, such as an annotation, is required to define a managed bean.

Community
  • 1
  • 1
gurghet
  • 7,591
  • 4
  • 36
  • 63
  • 1
    From which spec are you quoting? Managed beans or CDI? There's a difference. – Harald Wellmann Apr 09 '15 at 09:44
  • What's the difference? – gurghet Apr 10 '15 at 16:40
  • 1
    This is from the CDI spec. Think of Managed Beans as (super) classes with default features and CDI beans as specialized (child class ) version of the Managed Bean super class. Yes, you do not need any annotation to define a Managed Bean. A valid managed bean enjoys the following services rendered to it by the container - life cycle management, Resource injection and interceptors. CDI builds on top of the managed bean spec and provides additional services - mainly contextual dependency injection. To convert a simple managed bean into a CDI bean, you need to define CDI specific annotations – Abhishek Apr 16 '15 at 07:02

1 Answers1

9

Sounds like you were used to CDI 1.0 and are now confused by implicit bean archives introduced in CDI 1.1.

beans.xml is now optional, and implicit bean archives are the default, which means that a class is a bean candidate only if it has a bean defining annotation like @Dependent, @RequestScoped etc.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • but the specs: A managed bean is implemented by a Java class, which is called its bean class [...] **No special declaration, such as an annotation, is required to define a managed bean**. – gurghet Apr 09 '15 at 09:23
  • 4
    You can turn **implicit bean archives** into **explicit bean archives** eg. by setting `bean-discovery-mode` to `all` in `beans.xml`: https://docs.jboss.org/cdi/spec/1.1/cdi-spec.html#bean_archive – Puce Apr 09 '15 at 09:40
  • 1
    The relevant part in the CDI 1.2 spec is here: http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#bean_archive – Puce Apr 09 '15 at 09:50
  • 1
    @gurghet By default CDI 1.1+ does not require you to create even empty beans.xml (as it was required by v1.0) *but* by default only beans marked with scope annotations are managed by the CDI container. – Arek Apr 17 '15 at 18:07