Spring&Hibernate配置(使用Maven):java.lang.ClassNotFoundException:org.hibernate.cfg.Configuration

我正在尝试在Weblogic 10.3服务器上运行的应用程序中包含spring和hibernate.当我在服务器上运行应用程序时,在访问TestServlet以检查我的配置时,出现以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in class path resource [spring-config/HorizonModelPeopleConnectionsSpringContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:448)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:284)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:91)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:75)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
    at view.com.horizon.test.SpringHibernateServlet.doGet(SpringHibernateServlet.java:27)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at oracle.security.wls.filter.SSOSessionSynchronizationFilter.doFilter(SSOSessionSynchronizationFilter.java:279)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at oracle.dms.wls.DMSServletFilter.doFilter(DMSServletFilter.java:326)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3592)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:756)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:721)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:384)
    ... 31 more
Caused by: java.lang.NoClassDefFoundError: org.hibernate.cfg.Configuration
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.class$(LocalSessionFactoryBean.java:158)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.(LocalSessionFactoryBean.java:158)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:85)
    ... 35 more

我已经检查了我的应用程序,并且包含了hibernate jar文件,它包含它说缺少的类:org.hibernate.cfg.Configuration.

该应用程序是使用maven构建的.这些是使用spring和hibernate的JAR文件的依赖项:

    <!-- Frameworks -->
    <!-- Hibernate framework -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate</artifactId>
  <version>3.2.7.ga</version>
</dependency>
<!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.2</version>
</dependency>
<!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
<dependency>
  <groupId>javassist</groupId>
  <artifactId>javassist</artifactId>
      <version>3.4.GA</version>
</dependency>
    <!-- Spring framework -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-orm</artifactId>
  <version>2.5.6</version>
</dependency>

起初我以为spring和hibernate库中的版本可能是个问题,所以我尝试了不同的版本,但是仍然找不到任何地方指出哪个库版本兼容.刚知道Spring 2.5.x需要休眠> = 3.1

这是我的Spring配置文件:

<?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-2.5.xsd">
    <bean id="myDataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>jdbc/WebCenterDS</value>
        </property>
        <!--property name="resourceRef">
            <value>true</value>
        </property>
        <property name="jndiEnvironment">
            <props>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                <prop key="java.naming.provider.url">t3://localhost:7001</prop>
            </props>
        </property-->
    </bean>
    <bean id="mySessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate-config/hibernate.cfg.xml</value>
        </property>
        <property name="mappingResources">
            <list>
                <value>classpath:com/horizon/model/peopleconnections/profile/internal/bean/CustomAttribute.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
        </property>
    </bean>
    <bean id="profileExtensionDAO"
          class="com.horizon.model.peopleconnections.profile.internal.dao.ProfileExtensionDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
</beans>

我得到的WAR结构如下:

J2EETestApplication
│   springhibernate.jsp
│
└───WEB-INF
    │   faces-config.xml
    │   web.xml
    │   weblogic.xml
    │
    ├───classes
    │   └───view
    │       └───com
    │           └───horizon
    │               └───test
    │                       SpringHibernateServlet.class
    │
    └───lib
            activation-1.1.jar
            antlr-2.7.6.jar
            aopalliance-1.0.jar
            asm-1.5.3.jar
            asm-attrs-1.5.3.jar
            cglib-2.1_3.jar
            commons-codec-1.3.jar
            commons-collections-2.1.1.jar
            commons-logging-1.1.1.jar
            dom4j-1.6.1.jar
            ehcache-1.2.3.jar
            hibernate-3.2.7.ga.jar
            horizon-model-commons-1.0-SNAPSHOT.jar
            horizon-model-peopleconnections-1.0-SNAPSHOT.jar
            horizon-shared-commons-1.0-SNAPSHOT.jar
            horizon-shared-logging-1.0-SNAPSHOT.jar
            horizon-shared-util-1.0-SNAPSHOT.jar
            horizon-shared-webcenter-1.0-SNAPSHOT.jar
            horizon-shared-webcenter.jar
            httpclient-4.0.1.jar
            httpcore-4.0.1.jar
            javassist-3.4.GA.jar
            jta-1.0.1B.jar
            log4j-1.2.14.jar
            mail-1.4.1.jar
            peopleconnections-profile-model-11.1.1.2.0.jar
            saxon-9.1.0.8.jar
            serviceframework-11.1.1.2.0.jar
            slf4j-api-1.5.2.jar
            slf4j-log4j12-1.5.2.jar
            spring-beans-2.5.6.jar
            spring-context-2.5.6.jar
            spring-core-2.5.6.jar
            spring-orm-2.5.6.jar
            spring-tx-2.5.6.jar

我缺少任何依赖项或配置吗?

如果我在没有弹簧的情况下使用休眠方式(使用相同的库),则不会得到ClassDefNotFoundException:

导入java.net.URL;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            URL hibernateConfigURL = HibernateUtil.class.getClassLoader().getResource("hibernate-config/hibernate.cfg.xml");
            return new Configuration().configure(hibernateConfigURL).buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

这使我认为休眠库未正确选择不是问题.在开始从ApplicationContent构建Bean时,似乎正确地选择了Spring库.这两个库相互看不见是一个问题吗?

解决方法:

我的猜测是您的应用程序未在Web应用程序中使用Spring JAR,而是由WebLogic提供的,而那些在战争中无法“看到” Hibernate JAR的应用程序.

要告诉Weblogic查看war文件,请提供一个WEB-INF / weblogic.xml,并将preferred-web-inf-classes设置为true(由于WLS中捆绑了ANTLR版本,因此Hibernate仍然需要这样做):

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bea.com/ns/weblogic/90 http://www.bea.com/ns/weblogic/90/weblogic-web-app.xsd">
  <context-root>/myApp</context-root>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>
上一篇:Java-Weblogic EAR类加载


下一篇:在Linux服务器、客户端之间构建密钥对验证进行远程连接