Announcement Announcement Module
Collapse
No announcement yet.
NoSuchBeanDefinition confusion Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NoSuchBeanDefinition confusion

    Hi everybody. Firstly I want to wish everyone in this community a very Happy New Year.

    Secondly, I need some help with a very basic piece of code. I am trying to run a test for one of my DAOs, The object being tested is the HibernateTrickTypeDao, however when I run the test, I will encounter a NoSuchBeanDefinitionException. The test will only pass if the type I attempt to autowire is a TrickTypeDao. Shouldn't Spring be able to create a HibernateTrickTypeDao when I used the @Repository annotation in the class? I am slightly confused here. Any thoughts?

    Stacktrace
    Code:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean 
    of type [zing.dao.hibernate.HibernateTrickTypeDao] found for dependency: expected at least 1 bean which 
    qualifies as autowire candidate for this dependency. 
    Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    	... 28 more
    TestHibernateTrickTypeDao.xml
    (the xml namespaces are left out on purpose)
    Code:
    <beans>
        <context:annotation-config/>
        <context:component-scan base-package="zing"/>
        <tx:annotation-driven transaction-manager="transactionManager"/>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="annotatedClasses">
                <list>
                    <value>zing.domain.TrickType</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <value>
                    hibernate.format_sql=true
                    hibernate.show_sql=true
                </value>
            </property>
        </bean>
        
        <jdbc:embedded-database id="dataSource" type="HSQL">
            <jdbc:script location="classpath:testdb/schema.sql"/>
            <jdbc:script location="classpath:testdb/testdata.sql"/>
        </jdbc:embedded-database>
    
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>    
    </beans>
    HibernateTrickTypeDao.java
    Code:
    package zing.dao.hibernate;
    
    /*Import statements here*/
    
    @Repository
    public class HibernateTrickTypeDao implements TrickTypeDao {
    	
    	@Autowired
    	public HibernateTrickTypeDao(SessionFactory sessionFactory) {
    		super(sessionFactory);
    	}
    
    	@Transactional(readOnly = true)
    	@Override
    	public List<TrickType> findAll() {
    		return this.sessionFactory.createQuery("FROM TrickType").list();
    	}
    }
    TestHibernateTrickTypeDao.java
    Code:
    package zing.dao.hibernate;
    
    import zing.dao.TrickTypeDao;
    import zing.domain.TrickType;
    
    /*Other import statements here*/
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class TestHibernateTrickTypeDao {
    	
    	@Autowired
    	private HibernateTrickTypeDao dao;  /* encountered NoSuchBeanDefinitionException */
    //	private TrickTypeDao dao;  /* test runs fine with this */        
    
    	@Test
    	public void testFindAll() {
    		
    		List<TrickType> types = dao.findAll();
    		for(TrickType type : types) {
    			System.out.println(type.toString());
    		}
    	}
    }

  • #2
    No it shouldn't... I suggest a read of the aop section of the reference guide, especially the part that explains proxies... Spring will create a proxy acting like a TrickTypeDao (NOT a HibernateTrickTypeDao) because by default it uses JDK Dynamic proxies which only create proxies for interfaces.

    But as I stated read the aop section of the reference guide.

    Comment

    Working...
    X