Announcement Announcement Module
Collapse
No announcement yet.
2 beans of same type not allowed in application context? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2 beans of same type not allowed in application context?

    Putting design considerations aside (I don't have time to refactor all this), I am in a situation where I want two beans of the same type that just use different constructors so that one of them doesn't inject certain beans (in a hope to solve a circular dependency problem) In my application context xml, I have something like this:
    Code:
    <bean id="problemBean" class="com.blah.ProblemBean" scope="singleton" >
    	<constructor-arg ref="bean8" />
    	<constructor-arg ref="bean9" />
    	<constructor-arg ref="populateOnlyFoo" />
    </bean>
    
    <bean id="populateOnlyFoo" class="com.blah.Foo" scope="singleton">
    	<constructor-arg ref="bean1" />
    	<constructor-arg ref="bean2" />
    	<constructor-arg ref="bean3" />
    </bean>
    
    <bean id="Foo" class="com.blah.Foo" scope="singleton">
    	<constructor-arg ref="bean1" />
    	<constructor-arg ref="bean2" />
    	<constructor-arg ref="bean3" />
    	<constructor-arg ref="bean4" />
    	<constructor-arg ref="bean5" />
    	<constructor-arg ref="bean6" />
    	<constructor-arg ref="bean7" />
    </bean>
    The problem is when it tries to instantiate problemBean. I get the error:
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [com.blah.Foo] is defined: expected single matching bean but found 2: [populateOnlyFoo, Foo]

    In the XML I'm telling it exactly which one to use. Why can't it see that I want populateOnlyFoo in there? Is there some attribute I can set that will not cause this to error? It seems like I should be able to do this. Any information on this would be much appreciated.

  • #2
    Hi,
    This is a common error when doing Autowiring by type. Spring has provided a workaround for this.
    Use @Qualifier Annotation

    So in ur code u hav to include this line
    @Qualifier("populateOnlyFoo")

    Regards

    Comment


    • #3
      Thank you for your response. I'm not sure I fully understand though. Where exactly do I put that annotation? On the constructor argument? On the ProblemBean class? To be clear, I don't use the @Autowire annotation anywhere, and I don't want to use it anywhere. I don't know if the makes a difference for the @Qualifier annotation. I'm just not sure I understand how the @Qualifier annotation works. Any more information would be great! Thanks.

      Comment


      • #4
        Ok, so I lied. Apparently there is one place using the @Autowire annotation after all. There was a controller that had:
        Code:
        @Autowired
        private Foo foo = null;
        I changed it to be:
        Code:
        @Autowired
        @Qualifier("Foo")
        private Foo foo = null;
        And now it all works just fine. That all makes sense now. I just need to look at the logs closer..... Sorry and thanks again for your help!

        Comment

        Working...
        X