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

  • UnsatisfiedDependencyException: Unsatisfied dependency

    This is my Test class
    Code:
    public class Test extends AbstractJpaTests {
        // I hope this userDao will be automatically injected
        private UserDao userDao = null;
        // actually I don't need this dataSource, but the exception seems to need it
        // private DataSource dataSource;  // dataSource property exists already
    
        public void testUserDao() {
            logger.info("Fine");
            this.assertNotNull(userDao);
        }
        
        // protected String[] getConfigurations() {  // I must be asleep this afternoon. This method name must be getConfigLocations
        protected String[] getConfigLocations() {
            setDependencyCheck(false);
            setAutowireMode(AUTOWIRE_BY_NAME);
            return new String[] {
                    "classpath:applicationContext-resources.xml",
                    "classpath:applicationContext-services.xml"
            };
        }
    
        public void setUserDao(UserDao dao) {
            userDao = dao;
        }
    }
    This is the content of my applicationContext-*.xml
    Code:
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
            <property name="username" value="hannah"/>
            <property name="password" value="hannah"/>
        </bean>
    
        <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="showSql" value="true"/>
                    <property name="generateDdl" value="true"/>
                    <property name="databasePlatform" value="org.hibernate.dialect.Oracle9Dialect"/>
                </bean>
            </property>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="userDao" class="com.momentumsystems.acegiex.dao.jpa.UserDaoJpa" />
    And this is the stack trace:
    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.momentumsystems.acegiex.dao.Test': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:1019)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:839)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:273)
    	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:88)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:47)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:115)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:180)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:153)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:111)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:159)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:239)
    	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
    The exception said "Error creating bean with name 'com.momentumsystems.acegiex.dao.Test': Unsatisfied dependency expressed through bean property 'dataSource': Set this property value or disable dependency checking for this bean.". So I put a dataSource property in the Test class (I don't need the setDataSource method because that method exists in the AbstractAnnotationAwareTransactionalTests class). I even tried to disable dependency checking for this bean with setDependencyCheck(false) in the getConfigurations method, but it doesn't help.

    Could anybody help me please?: Problem solved (look at the comments I wrote).
    Last edited by dxxvi; Mar 15th, 2007, 03:50 PM. Reason: Problem solved.

  • #2
    I would guess that it's not your class that's the problem, one of the super-classes must require a dataSource. I'm not sure why it can't see the dataSource though, I'd presume that the applicationContext containing it isn't loaded. As for setDependencyCheck, have you tried setting this in the constructor instead.
    http://www.springframework.org/docs/...Check(boolean)

    Comment


    • #3
      This might be related to the classpath. most likely dependencies of your datasource are not in classpath.

      Comment


      • #4
        No it isn't related to the classpath. Per default the testcases do dependency injection by type and check for all dependencies to be set. You tried to disable the checking in the getConfigurations method, however that is to late, dependencies are checked BEFORE the call to the getConfigurations.

        You need to disable it in your constructor.

        Code:
        public class Test extends AbstractJpaTests {
        
          public Test() {
            setDependencyCheck(false);
            setAutowireMode(AUTOWIRE_BY_NAME);  
          }
        }
        Setting those settings in any other method is to late, you need to set(/override) those in your constructor.

        Comment


        • #5
          dxxvi,
          I still say it's a classpath issue, check if all thease jars are in your classpath:
          antlr.jar, cglib.jar, asm.jar, asm-attrs.jars, commons-collections.jar, commons-pool.jar and jta.ja

          Comment


          • #6
            No it isn't a classpath issue... A classpath issue would result in ClassNotFound or NoClassDefFoundExceptions not in an unsatisfied dependecy!

            Comment


            • #7
              I was able to get pass this exception by making sure all the configuration xml files were in classpath.

              -Niraj.

              Comment


              • #8
                Possible Solution

                I've seen this error when WEB-INF/classes directory was somehow removed from the Eclipse classpath.

                Comment


                • #9
                  Originally posted by Marten Deinum View Post
                  No it isn't related to the classpath. Per default the testcases do dependency injection by type and check for all dependencies to be set. You tried to disable the checking in the getConfigurations method, however that is to late, dependencies are checked BEFORE the call to the getConfigurations.

                  You need to disable it in your constructor.

                  Code:
                  public class Test extends AbstractJpaTests {
                  
                    public Test() {
                      setDependencyCheck(false);
                      setAutowireMode(AUTOWIRE_BY_NAME);  
                    }
                  }
                  Setting those settings in any other method is to late, you need to set(/override) those in your constructor.

                  Thank you for infos.
                  With best regards.

                  Comment


                  • #10
                    Did you end up finding a solution to this problem?

                    Comment


                    • #11
                      Originally posted by niraj121 View Post
                      I was able to get pass this exception by making sure all the configuration xml files were in classpath.

                      -Niraj.
                      This is what worked for me as well.

                      sp

                      Comment


                      • #12
                        Solution:It's a bean wiring problem

                        The problem of the Unsatisfied Dependency is solved when the appropriate Dao or bean name is defined in the ApplicationContext.xml file so that the dependencies r resolved before instances r created.

                        Comment

                        Working...
                        X