Announcement Announcement Module
No announcement yet.
TestingError: UnsatisfiedDependencyException Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • TestingError: UnsatisfiedDependencyException

    I am quite new to Spring and think its a real cool framework, but i encountered some problems during testing my Services:

    I've made a general BaseClass extending "AbstractTransactionalSpringContextTests" and overwrote the method getConfigLocations with my SpringConfig.xml. This worked fine for DAOSupport, aber but now i have this UnsatisfiedDependencyException i can not understand.

    Full Stack Trace:
    org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name '' defined in null:
    Unsatisfied dependency expressed through bean property 'addressService': There are 2 beans of type [class] for autowire by type.
    There should have been 1 to be able to autowire property 'addressService' of bean ''.

    at AutowireCapableBeanFactory.autowireByType(Abstract
    at AutowireCapableBeanFactory.populateBean(AbstractAu
    at AutowireCapableBeanFactory.autowireBeanProperties(
    at org.springframework.test.AbstractDependencyInjecti onSpringContextTests.setUp(AbstractDependencyInjec
    at junit.framework.TestCase.runBare( )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at )
    at junit.framework.TestSuite.runTest( 08)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(

    But i only have one bean called addressService:
    <bean id="addressService" parent="transactionInterceptorTemplate">
    <property name="target">
    <ref bean="addressServiceTarget"/>

    the AddressServiceTarget:

    <bean id="addressServiceTarget" class="" >
    <property name="addressDAO">
    <ref bean="addressDAO"/>
    <property name="lovService">
    <ref bean="lovService"/>
    <property name="zipCode2CityService">
    <ref bean="zipCode2CityService"/>

    I donīt know the Problem. Is there any way to solve this Probem??

    Thank you!

  • #2
    Make the target an inner bean. That is, make the contents of the "target" property value a bean definition rather than a reference.



    • #3
      So I changed it to:

               <property name="target">
      		<bean class="org.gbif.col2005.service.impl.TaxaManagerImpl">
      			<property name="taxaDAO" ref="taxaDAO"/>
      And it got rid of the error - hope this helps someone else


      • #4
        Using "autoproxying" approaches--which will become more common with Spring 2.0--also avoids the duplicate bean error.

        The old practice XX and XXTarget should usually be avoided in favour of the inner bean, with Spring 1.2.x also.


        • #5
          I am having a similar problem but moving the the "dataSource" bean into data access classes doesn't fix the problem for me. Any ideas?

          Here is the error:
          org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'com.mycompany.user.dao.UserDaoTest' defined in null: Unsatisfied dependency expressed through bean property 'userDao': set this property value or disable dependency checking for this bean
          at AutowireCapableBeanFactory.checkDependencies(Abstr

          Here is the test bean definitions:
          <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
          <property name="url"><value>${jdbc.url}</value></property>
          <property name="username"><value>${jdbc.username}</value></property>
          <property name="password"><value>${jdbc.password}</value></property>

          <bean id="userDao" class="com.mycompany.user.dao.jdbc.JdbcUserDao">
          <property name="dataSource"><ref local="dataSource"/></property>
          <property name="userGroupDao"><ref bean="userGroupDao"/></property>

          <bean id="userDaoTest" class="com.mycompany.user.dao.UserDaoTest">
          <property name="userDao"><ref bean="userDao"/></property>

          <bean id="userGroupDao" class="com.mycompany.user.dao.jdbc.JdbcUserGroupDa o">
          <property name="dataSource"><ref local="dataSource"/></property>


          • #6

            Is the use of ref="target" only for Spring 2.0?

            <bean id="jobRequestDAO"
            <property name="dataSource"> <ref bean="avianDS"/></property>

            that works for me in general.. but once I switched to trying to write a unit test with extends AbstractTransactionalDataSourceSpringContextTests. .. nothing works.

            I get the exception mentioned at the start of this thread (that is can't find a bean to autowire but it found 4 beans etc).

            I have 2 xml files application-DS.xml and application-DAO.xml.

            I tried to ref="avianDS" without adding 'bean' and that fails the sax parser.

            any hints?


            • #7

              So.. i got it to work.. but the only means I could do so was to put the dataSource bean, as well as the daos, as well as my transaction manager.. *all in one file*.

              Is there any way to have these work if my dataSource's are in an applicationDS.xml, and my DAOs are in an applicationDAO.xml. If I seperate the files, I get the 'found too many beans to autowire' problem (it seems to multiple in 2's).

              Last edited by Oreoferret; Aug 22nd, 2006, 10:31 AM.