创建bean sessionFactory Maven时出错

时间:2015-07-21 06:55:07

标签: java spring hibernate maven spring-mvc

我正在使用eclipse集成Spring和Hibernate4。我收到以下错误。我已经完成了之前为发布的问题提供的大多数anserws所做的事情。我一直试图解决这个问题。请帮忙

   Jul 21, 2015 11:29:29 AM
 org.springframework.context.support.AbstractApplicationContext refresh
     WARNING: Exception encountered during context initialization - cancelling refresh attempt
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path
 resource [application-config.xml]: Invocation of init method failed;
 nested exception is java.lang.NoClassDefFoundError:
 org/hibernate/annotations/common/reflection/ClassLoadingException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.lumiplan.hibernate.mainMethod.MainClass.main(MainClass.java:17)
     Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoadingException
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1404)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        ... 12 more
     Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.ClassLoadingException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 20 more
     Exception in thread "main" 
 org.springframework.beans.factory.BeanCreationException: Error
 creating bean with name 'sessionFactory' defined in class path
 resource [application-config.xml]: Invocation of init method failed;
 nested exception is java.lang.NoClassDefFoundError:
 org/hibernate/annotations/common/reflection/ClassLoadingException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.lumiplan.hibernate.mainMethod.MainClass.main(MainClass.java:17)
     Caused by: java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoadingException
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1404)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
        ... 12 more
     Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.ClassLoadingException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 20 more

应用-config.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- xmlns:context="http://www.springframework.org/schema/context" Uncomment and add your base-package here:-->


    <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/lumiplan" />
        <property name="username" value="root" />
        <property name="password" value="install" />
    </bean>

     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="format_sql">true</prop>
            </props>
        </property>

        <property name="annotatedClasses">
            <list>
                <value>com.lumiplan.hibernate.entity.City</value>
                <value>com.lumiplan.hibernate.entity.Country</value>
                <value>com.lumiplan.hibernate.entity.State</value>
            </list>
        </property>
    </bean>
    <bean id = "load" class="com.lumiplan.hibernate.dao.CallMethods">
        <property name = "sessionFactory" ref = "sessionFactory"></property>
    </bean>
</beans>

pom.xml


    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

会话工厂声明

  public class CallMethods {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

我的主要课程

public class MainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext AC = new   ClassPathXmlApplicationContext("application-config.xml");
        CallMethods c = (CallMethods) AC.getBean("app");

与Maven内置的Maven依赖项一起使用以下jar 4.1.7.RELEASE。

  弹簧 - 弹簧 - 背景弹簧 - 弹簧 - 弹簧 - 弹簧 - 核心   spring-exception spring-instrument spring-jdbc spring-orm spring-tx   spring-web spring-webmvc spring-test

1 个答案:

答案 0 :(得分:0)

您似乎错过了类路径中的hibernate-commons-annotations.jar。添加是对pom.xml

的依赖