title: Is @javax.annotation.ManagedBean a CDI bean defining annotation?
body: <h2>The Question</h2>
  
  <p>Given that the archive we deploy is an "implicit bean archive" (see below), using <a href="http://docs.oracle.com/javaee/7/api/javax/inject/Inject.html" rel="noreferrer"><code>@javax.inject.Inject</code></a> to inject a <a href="http://docs.oracle.com/javaee/7/api/javax/annotation/ManagedBean.html" rel="noreferrer"><code>@javax.annotation.ManagedBean</code></a> into another managed bean work in WildFly 8.1.0, but it won't work in GlassFish 4.0.1-b08 nor GlassFish 4.1-b13. GlassFish crash with this message:</p>
  
  <blockquote>
    <p>WELD-001408: Unsatisfied dependencies for type...</p>
  </blockquote>
  
  <p>Do I misunderstand the following outlined specifications or do GlassFish have a bug?</p>
  
  <h2>CDI 1.1 Part 1</h2>
  
  <p>CDI 1.1 (<a href="https://jcp.org/en/jsr/detail?id=346" rel="noreferrer">JSR-346</a>) section 12.1 "Bean Archives" says:</p>
  
  <blockquote>
    <p>An explicit bean archive is an archive which contains a beans.xml file
    [..]. An implicit bean archive is any other archive which contains one
    or more bean classes with a bean defining annotation [..].</p>
  </blockquote>
  
  <p>If then, my archive has no <code>beans.xml</code> descriptor file, I will still be able to use beans that have a "bean defining annotation". Question is, what is a bean defining annotation?</p>
  
  <p>The CDI specification section 2.5 "Bean defining annotations" says:</p>
  
  <blockquote>
    <p>Any scope type is a bean defining annotation.</p>
  </blockquote>
  
  <p>So that's pritty clear and that's all there is to it according to this section of the CDI specification. If I deploy an archive with no <code>beans.xml</code> descriptor file in it, then I can @Inject beans as long as they have an explicitly declared scope, <a href="http://docs.oracle.com/javaee/7/api/javax/enterprise/context/RequestScoped.html" rel="noreferrer"><code>@javax.enterprise.context.RequestScoped</code></a> for example. It works in both WildFly and GlassFish. However..</p>
  
  <h2>Managed Beans</h2>
  
  <p>The subset specification that all specifications within the Java EE technology stack must adhere to, Managed Beans (<a href="https://jcp.org/aboutJava/communityprocess/final/jsr316/index.html" rel="noreferrer">JSR-316</a>), has a "base model" in which <a href="http://docs.oracle.com/javaee/7/api/javax/annotation/ManagedBean.html" rel="noreferrer"><code>@javax.annotation.ManagedBean</code></a> do define a managed bean. The managed beans specification doesn't say that <code>@ManagedBean</code> makes the bean a plausible injection <em>target</em> for an injection point (i.e., field or parameter). The specification do say that the beans "can be used anywhere in a Java EE application" (section MB.1.2 "Why Managed Beans?") which in my ears sound like they should be injectable too.</p>
  
  <h2>Java EE 7 Umbrella specification</h2>
  
  <p>The Java EE 7 specification (<a href="https://jcp.org/en/jsr/detail?id=342" rel="noreferrer">JSR-342</a>) has this to say in section EE.5.24 "Support for Dependency Injection":</p>
  
  <blockquote>
    <p>Containers must support injection points annotated with the
    javax.inject.Inject annotation only to the extent dictated by CDI. Per
    the CDI specification, dependency injection is supported on managed
    beans.</p>
    
    <p>There are currently three ways for a class to become a managed bean:</p>
    
    <ol>
    <li>Being an EJB session bean component.</li>
    <li>Being annotated with the ManagedBean annotation.</li>
    <li>Satisfying the conditions described in the CDI specification.</li>
    </ol>
    
    <p>Classes that satisfy at least one of these conditions will be eligible
    for full dependency injection support as described in the CDI
    specification.</p>
  </blockquote>
  
  <p>There you go: @ManagedBean has "full dependency injection support". Not half or just a little bit of support. Yet, I'm not that sure exactly what "dependency injection support" is. But I think that the paragraph that follows describe it well enough:</p>
  
  <blockquote>
    <p>Component classes listed in Table EE.5-1 that satisfy the third
    condition above, but neither the first nor the second condition, can
    also be used as CDI managed beans if they are annotated with a CDI
    bean-defining annotation or contained in a bean archive for which CDI
    is enabled. However, if they are used as CDI managed beans (e.g.,
    injected into other managed classes), the instances that are managed
    by CDI may not be the instances that are managed by the Java EE
    container.</p>
  </blockquote>
  
  <p>Basically, what this paragraph says is that the second condition <strong>is</strong> CDI managed beans that may be injected into other managed classes (because the exception beans "can also").</p>
  
  <p>The umbrella specification and the managed beans specification has both made it somewhat clear that the CDI specification has the last word.</p>
  
  <h2>CDI 1.1 Part 2</h2>
  
  <p>The <code>@ManagedBean</code> annotation is only found mentioned in the CDI specification two times, both of which occur in chapter 11 which speaks of life cycle CDI events that a CDI extension can observe. Section 11.5.7 is one of the hits and define a <code>ProcessInjectionPoint</code> event. A managed bean may use dependency injection - no surprise there. However, section 11.5.8 define a <code>ProcessInjectionTarget</code> event. Here's what the specification has to say about the ProcessInjectionTarget event:</p>
  
  <blockquote>
    <p>The container must fire an event for every Java EE component class
    supporting injection that may be instantiated by the container at
    runtime, including every managed bean declared using @ManagedBean, EJB
    session or message-driven bean, bean, interceptor or decorator.</p>
  </blockquote>
  
  <p>This phrase says undoubtedly that a <code>@ManagedBean</code> may be used as target for an injection point without adding the notion of scope types (<a href="http://docs.oracle.com/javaee/7/api/javax/enterprise/context/Dependent.html" rel="noreferrer">@Dependent</a> is always default).</p>
  
  <p>As stated earlier, injecting a @ManagedBean from an implicit bean archive work in WildFly and as far as I can understand, this is required by all Java EE specifications just quoted. So I think it is GlassFish that has a bug. But the CDI spec never said a word about @ManagedBean in section 2.5 "Bean defining annotations", and as always, I'm a nervewreck when reading through overlapping Java EE specifications, so I thought I should ask before I go and file a "critical" bug to the GlassFish team.</p>
  
  <h2>EDIT 2014-08-22</h2>
  
  <p>Filed a GlassFish bug: <a href="https://java.net/jira/browse/GLASSFISH-21169" rel="noreferrer">https://java.net/jira/browse/GLASSFISH-21169</a>.</p>