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

  • New to Spring: UnsatisfiedDependencyException

    I just started with Spring. Therefore, i wrote a simple start application. It is so basic that i can show it to you completly.

    First off, i have a User class which represent userdate e.g. for a webpage login. For the ORM i use Hibernate and thus the class is appropriately annotated
    Code:
    package client.universal;
    
    @javax.persistence.Entity
    public class User {
    
    	@javax.persistence.Id
    	@javax.persistence.GeneratedValue( strategy= javax.persistence.GenerationType.AUTO )
    	public long id;
    	@javax.persistence.Column( nullable=false )
    	public String email;
    	@javax.persistence.Column( nullable=false )
    	public String password;
    	
    	public User() {
    		email = "Nothing yet.";
    		password = "Nothing yet.";
    	}
    	
    	public User( String email_, String password_ ) {
    		this.email = email_;
    		this.password = password_;
    	}
    
    	public String toString() {
    		return "\nemail = " + email + "\npassword = " + password + "\n";
    	}
    	
    	//getters and setters omitted
    	
    }
    Secondly i implemented a DAO layer.
    Code:
    package dao;
    
    import java.util.List;
    import client.universal.User;
    
    public interface UserDAOInterface {
    	public List<User> getUsers();
    	public void saveUser( User user );
    	public void getUser( long id );
    }
    and an implementation
    Code:
    package dao;
    
    import java.util.List;
    
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import client.universal.User;
    
    public class UserDAO extends HibernateDaoSupport implements UserDAOInterface {
    
    	@SuppressWarnings("unchecked")
    	public List<User> getUsers() {
    		return getHibernateTemplate().loadAll( User.class );
    	}
    	
    	public void saveUser( User user ) {
    		getHibernateTemplate().save( user );
    	}
    
    	public void getUser( long id ) {
    		getHibernateTemplate().get( User.class, id );
    	}
    }
    Okay, for being shure that everything is alright, i wrote a test case
    Code:
    package test;
    
    import java.util.List;
    import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
    import client.universal.User;
    import dao.*;
    
    
    public class UserDAOTest extends AbstractTransactionalDataSourceSpringContextTests {
    
    	private UserDAOInterface userDAO;
    	
    	public void testGetAllUsers1() {
    		List<User> users = userDAO.getUsers();
    		assertNotNull( users );
    	}
    	
    	public void testGetAllUsers2() {
    		List<User> users = userDAO.getUsers();
    		assertEquals( "[email protected]", users.get(0).email );
    	}
    }
    My applicationContext.xml looks like this
    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">
    
    	<bean id="user" class="client.universal.User" />
    	
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    		<property name="drivercClass" value="com.mysql.jdbc.Driver"/>
    		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/gjsh"/>
    		<property name="user" value="mysqluser"/>
    		<property name="password" value="password"/>
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    	</bean>
    	
    	<bean id="userDAO" class="dao.UserDAOInterface">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    </beans>
    When i try to run this testcase i get the following exception and the test fails:
    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test.UserDAOTest': 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:1184)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1006)
    	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:88)
    	at junit.framework.TestCase.runBare(TestCase.java:128)
    	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:120)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	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)
    I have no idea what this means.

  • #2
    Okay, i noticed that i forgot to add the following methods to my testcase
    Code:
            public void setUserDAOInterface( UserDAOInterface userDAO_ ) {
    		this.userDAO = userDAO_;
    	}
    	
    	@Override
    	protected String[] getConfigLocations() {
    		return new String[]{"applicationContext.xml"};
    	}
    I also altered my applicationContext.xml
    Code:
    <bean id="userDAO" class="dao.UserDAO">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    So instead of using an interface here i use the concret implementation now.



    But unfortunatly now i run into another problem. I get the following exception:
    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test.UserDAOTest': Unsatisfied dependency expressed through bean property 'transactionManager': Set this property value or disable dependency checking for this bean.

    Comment


    • #3
      Okay, i got it i added the follwing stuff to my applicationContext.xml (thanks Google)
      Code:
      <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
          	<property name="transactionManager" ref="transactionManager"/>
          </bean>
      
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          	<property name="dataSource" ref="dataSource"/>
        	</bean>
      Now my testcase works fine. Thanks for reading anyway.

      Sadly my book for starting spring does not explain this testcase correctly and does not show an appropriate configuration setting for a testcase like this.

      Comment

      Working...
      X