Hibernate SessionFactory注释错误

时间:2013-04-24 01:59:05

标签: java hibernate spring-mvc sessionfactory

获取此错误。尝试了一切来解决它。看了几天相似的问题,但没有结果。也许你们其中一个人可能会发现错误?先感谢您。

错误日志:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: miniVLE.dao.MiniVLEDAOImplementation miniVLE.service.StudentService.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'miniVLEDAOImplementation': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory miniVLE.dao.MiniVLEDAOImplementation.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.hibernate.SessionFactory] 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)}

我的调度员 - servlet:

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="miniVLE.controller" />
    <context:component-scan base-package="miniVLE.service" />
    <context:component-scan base-package="miniVLE.beans" />
    <context:component-scan base-package="miniVLE.dao" />
   <!-- <mvc:annotation-driven />-->
    <!--tx:annotation-driven transaction-manager="transactionManager" /-->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- Declare a view resolver-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />


    <!-- Connects to the database based on the jdbc properties information-->
    <bean id="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name ="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name ="url" value="jdbc:derby://localhost:1527/minivledb"/>
        <property name ="username" value="root"/>
        <property name ="password" value="123" />
    </bean>

     <!-- Declares hibernate object -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> ${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
        <!-- A list of all the annotated bean files, which are mapped with database tables-->
        <property name="annotatedClasses">
            <list>
                <value> miniVLE.beans.Course </value>
                <value> miniVLE.beans.Student </value>
                <value> miniVLE.beans.Department </value>  
                <value> miniVLE.beans.Module </value>  
                <value> miniVLE.beans.TimeSlot </value> 
            </list>
        </property>
    </bean>

    <!-- Declare a transaction manager-->
    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

我的DAO类实现:

import java.util.ArrayList;
import java.util.List;
import miniVLE.beans.Course;
import miniVLE.beans.Department;
import miniVLE.beans.Module;
import miniVLE.beans.Student;
import miniVLE.beans.TimeSlot;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class MiniVLEDAOImplementation implements MiniVLEDAO{

    // Used for communicating with the database
    @Autowired
    private SessionFactory sessionFactory; ... 

服务:

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudentService {

    @Autowired
    MiniVLEDAOImplementation dao;

1 个答案:

答案 0 :(得分:0)

哦,真的很傻!确保在applicationContext.xml

中不要加倍
<context:component-scan base-package="miniVLE.controller" />
<context:component-scan base-package="miniVLE.service" />
<context:component-scan base-package="miniVLE.beans" />
<context:component-scan base-package="miniVLE.dao" />
天啊..我杀了一天试图解决它!