使用Spring和Hibernate时的NoSuchBeanDefinitionException

时间:2017-05-26 09:51:17

标签: spring hibernate

我在尝试运行程序时遇到NoSuchBeanDefinitionException(:没有定义名为'org.springframework.orm.hibernate4.LocalSessionFactoryBean'的bean)但是Bean已经在Beans2.xml文件中定义了。课程如下。感谢。

PieceHibernateDAO.java

<?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-3.0.xsd">

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="sample.spring" />
    </bean>

    <bean class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/liftcomp_db" />
        <property name="username" value="root" />
        <property name="password" value="8K887h))00" />
    </bean>


 <bean id="PieceHibernateDAO" 
 class="sample.spring.chapter01.PieceHibernateDAO">
        <property name="sessionFactory" ref="org.springframework.orm.hibernate4.LocalSessionFactoryBean"/>
    </bean>



  <bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
        <property name="listp" ref="Piece"></property>              
   </bean>
   <bean id = "Company" class = "sample.spring.chapter01.Company"/>
   <bean id = "Family" class = "sample.spring.chapter01.Family"/>
   <bean id = "Piece" class = "sample.spring.chapter01.Piece"/>   
</beans>

Beans2.xml

Model.ids and Model.select(:id).map(&:id)

1 个答案:

答案 0 :(得分:1)

您创建一个标识为sessionFeactory的bean。这必须是ref属性中的值:

<property name="sessionFactory" ref="sessionFactory"/>

因为这是bean的id。