@Repository @Component注释的NoSuchBeanDefinitionException

时间:2014-12-23 02:07:56

标签: java spring hibernate

我正在开发spring hibernate项目。我得到一个错误“NoSuchBeanDefinitionException”,如果我没有提到bean名称和@Repository或@Component。 例如:@Repository(“HibernateDaoImpl”)工作正常。               @Repository发出错误。

INFO: Building new Hibernate SessionFactory
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'HibernateDaoImpl' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
    at com.amal.springdb.JdbcDemo.main(JdbcDemo.java:23)

package com.amal.springdb.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Circle {

    @Id
    private int id;
    private String name;

    public Circle(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Circle() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

package com.amal.springdb.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository  //("HibernateDaoImpl")
public class HibernateDaoImpl {

    @Autowired
    private SessionFactory sessionFactory;

    public int getCircleCount(){
        String hql = "select count(*) from Circle";
        Query query = getSessionFactory().openSession().createQuery(hql);
        int i =  ((Long)query.uniqueResult()).intValue();
        return i;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

package com.amal.springdb;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.amal.springdb.dao.HibernateDaoImpl;
import com.amal.springdb.dao.JdbcDaoImpl;
import com.amal.springdb.dao.SimpleJdbcDaoImpl;
import com.amal.springdb.model.Circle;

public class JdbcDemo {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");


        HibernateDaoImpl hibernateDaoImpl = (HibernateDaoImpl) context.getBean("HibernateDaoImpl");
        System.out.println(hibernateDaoImpl.getCircleCount());  
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config></context:annotation-config>

    <context:component-scan base-package="com.amal.springdb"></context:component-scan>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"></property>
        <property name="url" value="jdbc:derby://localhost:1527/db;create=true"></property>
        <property name="initialSize" value="2"></property>
        <property name="maxActive" value="5"></property>
    </bean>

    <bean id="SimpleJdbcDaoImpl" class="com.amal.springdb.dao.SimpleJdbcDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>    
        <property name="packagesToScan" value="com.amal.springdb.model"></property>
        <property name="hibernateProperties">   
            <props>
                 <prop key="dialect">org.hibernate.dialect.DerbyDialect</prop>
            </props>        
        </property>
    </bean>
</beans>

1 个答案:

答案 0 :(得分:2)

Spring默认bean名称基于on uncapitalized short class names。因此,请使用context.getBean("hibernateDaoImpl")或根据@Repository("HibernateDAOImpl") explicitly指定所需的bean名称。