Results 1 to 4 of 4

Thread: Exception: Class contains two beans of same type

  1. #1
    Join Date
    Oct 2007
    Posts
    26

    Default Exception: Class contains two beans of same type

    The test bean has two members of same type "Manager which is simple DTO

    Code:
    public class ManagerTest extends AbstractDependencyInjectionSpringContextTests {
    
        private Manager manager;
        
        public void setManager(Manager manager) {        
            this.manager = manager;
        }
        
        public Manager getManager() {
            return manager;
        }
        
        private Manager manager2;
        
        public void setManager2(Manager manager2) {        
            this.manager2 = manager2;
        }
        
        public Manager getManager2() {
            return manager2;
        }
        
    
        protected String[] getConfigLocations() {
            return new String[] {"applicationContext.xml"};
        }
        
        public void testApp() {
           // do some thing
        }
    }
    The manager class is simple

    Code:
    public class Manager
    { 
    // just a place holder 
    }
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    	
    	<bean id="manager"  class="com.cp.test.Manager" >	
    	</bean>
    
    	<bean id="manager2" class="com.cp.test.Manager" >	
    	</bean>
    	
    </beans>
    Exception log is

    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.cp.test.ManagerTest': Unsatisfied dependency expressed through bean property 'manager': : No unique bean of type [com.cp.test.Manager] is defined: expected single matching bean but found 2: [manager, manager2]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cp.test.Manager] is defined: expected single matching bean but found 2: [manager, manager2]
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1091)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:982)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
    	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:84)
    	at junit.framework.TestCase.runBare(TestCase.java:132)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:124)
    	at junit.framework.TestSuite.runTest(TestSuite.java:232)
    	at junit.framework.TestSuite.run(TestSuite.java:227)
    	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    	at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
    	at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cp.test.Manager] is defined: expected single matching bean but found 2: [manager, manager2]
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1076)
    	... 24 more
    Google search asked me to put autowire=byName in bean definitions but it did not help either. This is simple example, my actual code has two members, one is of type A and other is B which extends A.

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    8.3.6.2. Dependency Injection of test fixtures:
    The AbstractDependencyInjectionSpringContextTests classes uses autowire by type. Thus if you have multiple bean definitions of the same type, you cannot rely on this approach for those particular beans. In that case, you can use the inherited applicationContext instance variable and perform explicit lookups using (for example) a call to applicationContext.getBean("titleDao").

    If you don't want dependency injection applied to your test cases, simply don't declare any public setter methods. Alternatively, you can extend AbstractSpringContextTests - the root of the JUnit 3.8 integration testing support class hierarchy in the org.springframework.test package - which merely contains convenience methods to load Spring contexts and performs no Dependency Injection of the test fixture.

  3. #3
    Join Date
    Oct 2007
    Posts
    26

    Default

    Quote Originally Posted by denis.zhdanov View Post
    What if I need this functionality in business or service beans and not test bean.

  4. #4

    Default Solution (that worked for me)

    Use annotation @Resouce(name="id_of_bean_to_be_injected")
    This should remove the exception of "no unique bean definition found". At runtime, it tries to find the injected bean definition and finds two beans which can be used so it gives this error.
    For this reason, you have to specify name attribute with resource annotation and give the id of the bean which you want to inject with that particular instance, in name attriburte.

    eg. (in your case):

    Code:
    @Resouce(name="manager2")       // or manager, whichever you want to inject
    Manager mgr;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •