3 Replies Latest reply on Oct 29, 2014 11:17 AM by Martin Kouba

    Produces method with interface arg. fails when interface has 2 implementations

    Dieter Decavele Newbie

      With Weld 2.1.2 the code below gives me an instance of type OtherPro when I ask the WeldContainer an instance for type Pro without any annotation.  Shouldn't this be rather type MyProducer?

       

      If I move the producer method into a separate class I get a Weld exception saying:

      org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001318: Cannot resolve an ambiguous dependency between:
        - Managed Bean [class OtherPro] with qualifiers [@Def @Any],
        - Producer Method [Pro] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @Singleton public static Factory.create(@Def Pro)]
      
      
      

      It does not matter if I change the return type to MyProduer.

       

      I'm clearly missing something.  Anyone who can explain this behaviour?

       

      public interface Pro {
      
          public void bla();
      }
      
      @Target({ TYPE, METHOD, FIELD, PARAMETER })
      @Retention(RUNTIME)
      @Documented
      @Qualifier
      @Inherited
      public @interface Def {
      
      }
      
      @Singleton
      @Def
      public class OtherPro implements Pro {
      
          @Override
          public void bla() {
          }
      }
      
      public class MyProducer implements Pro {
      
          public MyProducer(final Pro pro) {
      
          }
      
          @Override
          public void bla() {
          }
      
          @Produces
          @Singleton
          public static Pro create(@Def final Pro pro) {
              return new MyProducer(pro);
          }
      }