Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-814

Resources injected to producer fields fail to be produced

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Rejected
    • Affects Version/s: 1.1.0.CR3
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Steps to Reproduce:
      Hide

      The following class fails to deploy on JBoss AS 6.

      import javax.annotation.Resource;
      import javax.enterprise.context.ApplicationScoped;
      import javax.enterprise.inject.Produces;
      import javax.inject.Named;
      import org.infinispan.manager.EmbeddedCacheManager;

      /**
      *

      • @author johnament
        */
        public class CacheManagerProducer {
        @Produces @ApplicationScoped @Named("appCache")
        @Resource(name="CacheManager/entity")
        EmbeddedCacheManager ecm;

      //@Produces @ApplicationScoped @Named("appCache")
      public EmbeddedCacheManager getEmbeddedCacheManager()

      { return ecm; }

      }

      If you swap the commented producer line it does work. It appears that @Resource injection is not happening at the correct time. Same if you make it @Stateless.

      Show
      The following class fails to deploy on JBoss AS 6. import javax.annotation.Resource; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.inject.Produces; import javax.inject.Named; import org.infinispan.manager.EmbeddedCacheManager; /** * @author johnament */ public class CacheManagerProducer { @Produces @ApplicationScoped @Named("appCache") @Resource(name="CacheManager/entity") EmbeddedCacheManager ecm; //@Produces @ApplicationScoped @Named("appCache") public EmbeddedCacheManager getEmbeddedCacheManager() { return ecm; } } If you swap the commented producer line it does work. It appears that @Resource injection is not happening at the correct time. Same if you make it @Stateless.
    • Workaround Description:
      Hide

      Use method level producers for resources.

      Show
      Use method level producers for resources.

      Description

      When you inject a resource in to a bean (session, managed, cdi, etc), the producer field that can be bound to it fails to deploy, at least under AS 6.

        Gliffy Diagrams

          Issue Links

            Activity

            Hide
            meetoblivion John Ament added a comment -

            Note that this is just one example. We first saw this in the seam jms module starting in CR1 of JBoss AS 6.

            Show
            meetoblivion John Ament added a comment - Note that this is just one example. We first saw this in the seam jms module starting in CR1 of JBoss AS 6.
            Hide
            pmuir Pete Muir added a comment -

            This is spec defined behavior. From 3.5

            "The container is not required to support resources with scope other than @Dependent. Portable applications should not define resources with any scope other than @Dependent."

            Show
            pmuir Pete Muir added a comment - This is spec defined behavior. From 3.5 "The container is not required to support resources with scope other than @Dependent. Portable applications should not define resources with any scope other than @Dependent."
            Hide
            meetoblivion John Ament added a comment -

            Ok, but I'm curious as to why it does allow it from a method producer? Or is it simply a case where the container knows that this is a resource because the field has the injection point, but doesn't know about the method? In which case, another bypass would simply be to define the resource injection point on a setter and leave the producer on the field?

            Show
            meetoblivion John Ament added a comment - Ok, but I'm curious as to why it does allow it from a method producer? Or is it simply a case where the container knows that this is a resource because the field has the injection point, but doesn't know about the method? In which case, another bypass would simply be to define the resource injection point on a setter and leave the producer on the field?
            Hide
            pmuir Pete Muir added a comment -

            The key difference between the two approaches above is that in resource producer field you are, in essence, "aliasing" that resource as a CDI bean with a specified qualifier and type. CDI is responsible for managing the lifecycle, and the only lifecycle that generally makes sense is dependent scoped. In the case of a resource injection + producer method, you are injecting a resource and taking control of it's lifecycle yourself via the producer method that exposes it.

            IOW in the second case you are taking responsibility for the lifecycle of the resource instance and thus can choose whatever scope you want.

            Show
            pmuir Pete Muir added a comment - The key difference between the two approaches above is that in resource producer field you are, in essence, "aliasing" that resource as a CDI bean with a specified qualifier and type. CDI is responsible for managing the lifecycle, and the only lifecycle that generally makes sense is dependent scoped. In the case of a resource injection + producer method, you are injecting a resource and taking control of it's lifecycle yourself via the producer method that exposes it. IOW in the second case you are taking responsibility for the lifecycle of the resource instance and thus can choose whatever scope you want.

              People

              • Assignee:
                pmuir Pete Muir
                Reporter:
                meetoblivion John Ament
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Development