org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead.

Spring JIRA | John Purcell | 1 decade ago
  1. 0

    I've been using integration test cases derived from AbstractTransactionalDataLoadingSpringContextTests and noticed the following behavior. Say I've got two XML definition files: Foo.xml: <?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.xsd"> <bean id="UserManagerTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"><ref local="UserManagerDataSource"/></property> </bean> <bean id="UserManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>DUMMY VALUE</value></property> <property name="url"><value>jdbc:hsqldb:mem:test</value></property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean> </beans> which I then want to override in my test with Bar.xml: <?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.xsd"> <!-- Alias all datasources to the test one --> <alias name="dataSource" alias="UserManagerDataSource"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"><ref local="dataSource"/></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property> <property name="url"><value>jdbc:hsqldb:mem:test</value></property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean> </beans> You would figure that UserManagerDataSource wouldn't be defined since I'm aliasing it in a later file. However, when I run my test I get: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:698) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:604) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:256) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:223) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:195) at junit.framework.TestCase.runBare(TestCase.java:125) at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Meaning that it's still defining the bean "UserManagerDataSource" as not being a complete alias. Is this the expected behavior? (and if so, can we get it put into the documentation?)

    Spring JIRA | 1 decade ago | John Purcell
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead.
  2. 0

    I've been using integration test cases derived from AbstractTransactionalDataLoadingSpringContextTests and noticed the following behavior. Say I've got two XML definition files: Foo.xml: <?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.xsd"> <bean id="UserManagerTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"><ref local="UserManagerDataSource"/></property> </bean> <bean id="UserManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>DUMMY VALUE</value></property> <property name="url"><value>jdbc:hsqldb:mem:test</value></property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean> </beans> which I then want to override in my test with Bar.xml: <?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.xsd"> <!-- Alias all datasources to the test one --> <alias name="dataSource" alias="UserManagerDataSource"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"><ref local="dataSource"/></property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property> <property name="url"><value>jdbc:hsqldb:mem:test</value></property> <property name="username"><value>sa</value></property> <property name="password"><value></value></property> </bean> </beans> You would figure that UserManagerDataSource wouldn't be defined since I'm aliasing it in a later file. However, when I run my test I get: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:698) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:604) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:256) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:223) at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:195) at junit.framework.TestCase.runBare(TestCase.java:125) at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Meaning that it's still defining the bean "UserManagerDataSource" as not being a complete alias. Is this the expected behavior? (and if so, can we get it put into the documentation?)

    Spring JIRA | 1 decade ago | John Purcell
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead.
  3. 0

    UnsatisfiedDependencyException with dataSource definition - Spring Forum

    spring.io | 1 year ago
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.onicomconsulting.core.bdo.enums.EnumerationCheck': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been 1 to be able to autowire property 'dataSource' of bean 'com.onicomconsulting.core.bdo.enums.EnumerationCheck'.
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Unsatisfied Dependency Exception: (Frameworks forum at JavaRanch)

    coderanch.com | 11 months ago
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.wonder.tests.TestUserService': Unsatisfied dependency expressed through bean property 'userServiceDao': Set this property value or disable dependency checking for this bean.
  6. 0

    中国Java开发网 - [求助]为AppFuse添加一个PersonDAOTest的单元测试,出现错误

    cjsdn.net | 9 months ago
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.appfuse.dao.PersonDAOTest' defined in null: Unsatisfied dependency expressed through bean property 'personDAO': set this property value or disable dependency checking for this bean

  1. serious2monkeys 30 times, last 1 month ago
3 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. org.springframework.beans.factory.UnsatisfiedDependencyException

    Error creating bean with name 'UserManagerIntTests': Unsatisfied dependency expressed through bean property 'dataSource': There are 2 beans of type [interface javax.sql.DataSource] for autowire by type. There should have been exactly 1 to be able to autowire property 'dataSource' of bean 'UserManagerIntTests'. Consider using autowire by name instead.

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType()
  2. Spring Beans
    AbstractAutowireCapableBeanFactory.autowireBeanProperties
    1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:698)
    2. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:604)
    3. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:256)
    3 frames
  3. Spring TestContext
    AbstractDependencyInjectionSpringContextTests.setUp
    1. org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:223)
    2. org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:195)
    2 frames
  4. JUnit
    TestCase.runBare
    1. junit.framework.TestCase.runBare(TestCase.java:125)
    1 frame
  5. Spring TestContext
    ConditionalTestCase.runBare
    1. org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    1 frame
  6. JUnit
    TestSuite.run
    1. junit.framework.TestResult$1.protect(TestResult.java:106)
    2. junit.framework.TestResult.runProtected(TestResult.java:124)
    3. junit.framework.TestResult.run(TestResult.java:109)
    4. junit.framework.TestCase.run(TestCase.java:118)
    5. junit.framework.TestSuite.runTest(TestSuite.java:208)
    6. junit.framework.TestSuite.run(TestSuite.java:203)
    6 frames
  7. JUnit3 Runner
    RemoteTestRunner.main
    1. org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    2. org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    3. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    4. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    5. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    6. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    6 frames