春天& hibernate配置(使用maven):java.lang.ClassNotFoundException:org.hibernate.cfg.Configuration

时间:2010-04-28 12:51:09

标签: java hibernate spring maven-2 weblogic

我试图在运行在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需要hibernate&gt; = 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

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

如果我在没有使用spring的情况下使用hibernate(使用相同的库)我没有得到ClassDefNotFoundException:

import 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;
    }
}

这让我觉得hibernate库没有被正确选择不是问题。 Spring Library似乎在从ApplicationContent开始构建bean时被正确选择。这两个图书馆之间没有见面是一个问题吗?

4 个答案:

答案 0 :(得分:4)

我的猜测是你的应用程序没有在webapp中使用Spring JAR,而是WebLogic提供的那些,并且那些不能在战争中“看到”Hibernate JAR。

要告诉Weblogic查看war文件,请提供WEB-INF/weblogic.xml prefer-web-inf-classes设置为true(这是Hibernate所必需的,因为WLS捆绑了ANTLR版本):

<?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>

答案 1 :(得分:0)

你已经错过了hibernate-core依赖...文档中的例子。

http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html#tutorial-firstapp-mvn

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

答案 2 :(得分:0)

我编译'org.hibernate:hibernate-core:4.1.9'

我将它替换为编译'org.hibernate:hibernate-core:3.3.2.GA'

这是gradle语法,转换为maven并不困难

答案 3 :(得分:0)

除了你的答案@Rafeeq之外,这样做的伎俩应该是你在pom.xml上使用Maven依赖

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
        </dependency>