3 Replies Latest reply on May 12, 2009 9:37 AM by Gavin King

    AmbiguousDependencyException when using inheritance between simple beans

    Vivian Steller Newbie

      Hi!
      I'm taking the first steps with webbeans (1.0.0.PREVIEW1) and face an AmbiguousDependencyException at application startup.


      Scenario


      Consider the following simple example:



      • two classes, one extends the other

      • both should be simple beans, but should have a different scope

      • both should be named beans so that I can refer to them using EL

      • one class gets instances of both classes injected



      Speaking in code...


      @Named
      @ApplicationScoped
      public class PocInfo implements Serializable {
      ...
      }
      
      @Named
      @ConversationScoped
      public class PocInfoGuess extends PocInfo {
      ...
      }
      
      @Named
      @ConversationScoped
      public class FunctionalityCheck implements Serializable {
      
          @New
          private PocInfo pocInfo;
      
          @New
          private PocInfoGuess pocInfoGuess;
      ...
      }
      



      Actual result: AmbiguousDependencyException


      On startup (using jetty:run), I get the following exception:


      2009-05-07 14:56:41.889::WARN:  Failed startup of context org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@a2d72e{/poc-webbeans-servlet,/Users/brainbug/Projekte/openconfigurator/sandbox/pocs/webbeans/trunk/webapp/src/main/webapp}
      javax.inject.AmbiguousDependencyException: The injection point   Field @New private pocInfo on Annotated class Class @Named @ConversationScoped FunctionalityCheck
        Constructor public FunctionalityCheck();
        Method String public check();
        Method int public native hashCode();
        Method Class public final native getClass();
        Method boolean public equals(Object);
        Method String public toString();
        Method void public final wait(long, int);
        Method void public final native wait(long);
        Method void public final wait();
        Method void public final native notify();
        Method void public final native notifyAll();
       with binding types [@New] in Conversation scoped simple bean 'functionalityCheck' info.openconfigurator.poc.webbeans.FunctionalityCheck,  API types = [Object, Serializable, FunctionalityCheck], binding types = [@Current] has ambiguous dependencies
           at org.jboss.webbeans.BeanValidator.validate(BeanValidator.java:116)
           at org.jboss.webbeans.bootstrap.WebBeansBootstrap.boot(WebBeansBootstrap.java:207)
           at org.jboss.webbeans.environment.servlet.Listener.contextInitialized(Listener.java:132)
           at 
      ...
      



      What I expected


      Well, I think this is obvious: in FunctionalityCheck I just wanted to inject two instances of different type. Whether or not it is nice OO design and Web beans like to extend the injected types in order to apply different scopes to them - I would have expected that this simply works.


      Questions




      • why doesn't this work? ;)

      • why is this ambigious? In Seam this would have been working since both injections would have been resolved by their names, don't they?

      • how to use the same simple bean with different scopes?



      Thanks in advance for any hint!
      BTW. I already love Web Beans simply by reading the docs! Great, great work!


      Cheers,
      vivian