spring和hibernate集成类没有找到异常

时间:2012-09-14 03:50:21

标签: java spring hibernate

我是春天的新手,并尝试用spring进行hibernate工作。我每次都得到以下类没有找到异常时,我尝试了几个小时来完成这项工作。

> Exception in thread "main"
> org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
> find class
> [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]
> for bean with name 'sessionFactory' defined in class path resource
> [mad/springnhibernate/model/spring.xml]; nested exception is
> java.lang.ClassNotFoundException:
> org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1262)
>   at
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
>   at
> org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1331)

Followiing是我的spring.xml文件。你能解释一下这有什么问题吗?

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

    <context:component-scan base-package="mad.springnhibernate"/>
    <!-- Initialization for data source -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springnhibernate" />
        <property name="username" value="root" />
        <property name="password" value="pass" />
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="mad.springnhibernate.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

</beans>

感谢。

1 个答案:

答案 0 :(得分:1)

我认为你缺少org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean的jar文件。你应该包括其中一个罐子。找 http://www.jarfinder.com/index.php/java/info/org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean