Spring 3和Hibernate 4配置问题

时间:2014-11-29 10:46:15

标签: spring hibernate

我一直在为我的项目使用Spring 3和Hibernate 4。我已经添加了所有依赖项来支持Spring 3和hibernate 4项目。我不是特定于hibernate的版本,但它应该支持Spring 3.

当我尝试在Tomcat7项目上部署时,我遇到了以下异常。

  

引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private org.hibernate.SessionFactory com.sr.kgvk.utils.SessionFactoryImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为' sessionFactory'的bean时出错在ServletContext资源[/WEB-INF/HelloWeb-servlet.xml]中定义:init方法的调用失败;嵌套异常是java.lang.IncompatibleClassChangeError:实现类                   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)                   在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)                   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)                   ......还有68个               引起:org.springframework.beans.factory.BeanCreationException:创建名称为' sessionFactory'的bean时出错在ServletContext资源[/WEB-INF/HelloWeb-servlet.xml]中定义:init方法的调用失败;嵌套异常是java.lang.IncompatibleClassChangeError:实现类                   在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)                   在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)                   在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)                   at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)                   在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)                   在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)                   在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)                   在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)                   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)                   在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)                   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)                   ......还有70多个               引起:java.lang.IncompatibleClassChangeError:实现类                   at java.lang.ClassLoader.defineClass1(Native Method)                   at java.lang.ClassLoader.defineClass(ClassLoader.java:800)                   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)                   在org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)                   在org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)                   在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)                   在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)                   at java.lang.Class.getDeclaredConstructors0(Native Method)                   at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)                   at java.lang.Class.getConstructor0(Class.java:2885)                   at java.lang.Class.getDeclaredConstructor(Class.java:2058)                   在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)                   在org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:779)                   在org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:561)                   在org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)                   在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)                   在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)                   ......还有80多个

        Nov 29, 2014 3:53:58 PM org.apache.catalina.core.StandardContext loadOnStartup
        SEVERE: Servlet /mvc threw load() exception
        java.lang.IncompatibleClassChangeError: Implementing class
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
            at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
            at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
            at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
            at java.lang.Class.getDeclaredConstructors0(Native Method)
            at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
            at java.lang.Class.getConstructor0(Class.java:2885)
            at java.lang.Class.getDeclaredConstructor(Class.java:2058)
            at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
            at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:779)
            at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:561)
            at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)






Spring Configuration file for hibernate4

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


           <context:component-scan base-package="com.sr.kgvk" />
           <mvc:annotation-driven/>

           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/jsp/" />
              <property name="suffix" value=".jsp" />
           </bean>


           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">
           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
           <property name="prefix" value="/WEB-INF/views/"></property>
           <property name="suffix" value=".jsp"></property>
           </bean>

             <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                    <property name="location" value="classpath:db.properties" />
             </bean>

             <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    <property name="driverClassName" value="${jdbc.driverClassName}" />
                    <property name="url" value="${jdbc.databaseurl}" />
                    <property name="username" value="${jdbc.username}" />
                    <property name="password" value="${jdbc.password}" />
             </bean>

             <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
                <property name="dataSource" ref="dataSource" />
                <property name="configLocation">
                    <value>classpath:hibernate.cfg.xml</value>
                </property>

                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">
                            ${hibernate.dialect}
                        </prop>
                        <prop key="hibernate.show_sql">
                            ${hibernate.show_sql}
                        </prop>
                        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                        <prop key="hibernate.cache.use_second_level_cache">true</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                    <prop key="hibernate.cache.provider_configuration_file_resource_path">hibernate-ehcache.xml</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.generate_statistics">true</prop>
                    <prop key="hibernate.cache.use_structured_entries">true</prop> 
                    </props>
                </property>
             </bean>


            <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
            </bean>

            <tx:annotation-driven/>

        </beans>

Here is the 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">
              <modelVersion>4.0.0</modelVersion>

              <groupId>webproject</groupId>
              <artifactId>mvc</artifactId>  
              <version>1.0</version>
              <packaging>war</packaging>

              <name>mvc</name>
              <url>http://maven.apache.org</url>    


              <properties>
                    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                    <maven.compiler.source>1.7</maven.compiler.source>
                    <maven.compiler.target>1.7</maven.compiler.target>
                    <spring.version>3.2.4.RELEASE</spring.version>
                    <spring.security.version>3.2.4.RELEASE</spring.security.version>
                    <junit.version>4.11</junit.version>
                    <failsafe.plugin.version>2.16</failsafe.plugin.version>
                    <jstl.version>1.2</jstl.version>
                    <mysql-connector-java.version>5.1.29</mysql-connector-java.version>
                    <spring-tx.version>4.0.1.RELEASE</spring-tx.version>
                    <spring-jdbc.version>4.0.1.RELEASE</spring-jdbc.version>
                </properties>

                <build>
                    <finalName>mvc</finalName>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>build-helper-maven-plugin</artifactId>
                            <version>1.7</version>
                            <executions>
                                <execution>
                                    <id>add-source</id>
                                    <phase>generate-sources</phase>
                                    <goals>
                                        <goal>add-source</goal>
                                    </goals>
                                    <configuration>
                                        <sources>
                                            <source>src/config</source>
                                        </sources>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-war-plugin</artifactId>
                            <version>2.3</version>
                            <configuration>
                                <failOnMissingWebXml>false</failOnMissingWebXml>
                            </configuration>
                        </plugin>

                        <plugin>
                            <groupId>org.apache.tomcat.maven</groupId>
                            <artifactId>tomcat7-maven-plugin</artifactId>
                            <configuration>
                                <!-- <path>/${project.artifactId}</path> -->
                                <path>/</path>
                                <port>8080</port>
                            </configuration>
                        </plugin>

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.16</version>
                            <configuration>
                                <includes>
                                    <include>**/*.class</include>
                                </includes>
                            </configuration>
                        </plugin>

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-failsafe-plugin</artifactId>
                            <version>${failsafe.plugin.version}</version>
                            <dependencies>
                                <dependency>
                                    <groupId>org.apache.maven.surefire</groupId>
                                    <artifactId>surefire-junit47</artifactId>
                                    <version>${failsafe.plugin.version}</version>
                                </dependency>
                            </dependencies>

                            <executions>
                                <execution>

                                    <configuration>
                                        <includes>
                                            <include>**/*.class</include>
                                        </includes>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>

                    <pluginManagement>
                        <plugins>
                            <plugin>
                                <groupId>org.apache.tomcat.maven</groupId>
                                <artifactId>tomcat7-maven-plugin</artifactId>
                                <version>2.0</version>
                            </plugin>
                            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                            <plugin>
                                <groupId>org.eclipse.m2e</groupId>
                                <artifactId>lifecycle-mapping</artifactId>
                                <version>1.0.0</version>
                                <configuration>
                                    <lifecycleMappingMetadata>
                                        <pluginExecutions>
                                            <pluginExecution>
                                                <pluginExecutionFilter>
                                                    <groupId>
                                                        org.codehaus.mojo
                                                    </groupId>
                                                    <artifactId>
                                                        build-helper-maven-plugin
                                                    </artifactId>
                                                    <versionRange>
                                                        [1.7,)
                                                    </versionRange>
                                                    <goals>
                                                        <goal>add-source</goal>
                                                    </goals>
                                                </pluginExecutionFilter>
                                                <action>
                                                    <ignore></ignore>
                                                </action>
                                            </pluginExecution>
                                        </pluginExecutions>
                                    </lifecycleMappingMetadata>
                                </configuration>
                            </plugin>
                        </plugins>
                    </pluginManagement>
                            <testResources>
                        <testResource>
                            <directory>src/test/resources</directory>
                            <filtering>true</filtering>
                        </testResource>
                    </testResources>
                </build>

            <dependencies>

                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>javax.servlet-api</artifactId>
                        <version>3.0.1</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>jstl</artifactId>
                        <version>1.2</version>
                    </dependency>


                    <dependency>
                        <groupId>org.apache.velocity</groupId>
                        <artifactId>velocity</artifactId>
                        <version>1.7</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-dbcp</groupId>
                        <artifactId>commons-dbcp</artifactId>
                        <version>1.4</version>
                    </dependency>

                    <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.29</version>
                    </dependency>

                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                        <version>1.7.2</version>
                    </dependency>

                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>${junit.version}</version>
                        <scope>test</scope>
                    </dependency>

                    <dependency>
                        <groupId>commons-lang</groupId>
                        <artifactId>commons-lang</artifactId>
                        <version>2.6</version>
                    </dependency>

                    <dependency>
                        <groupId>org.apache.httpcomponents</groupId>
                        <artifactId>httpclient</artifactId>
                        <version>4.3.1</version>
                    </dependency>

                   <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>3.8.1</version>
                    <scope>test</scope>
                   </dependency>

                    <dependency>
                        <groupId>jstl</groupId>
                        <artifactId>jstl</artifactId>
                        <version>${jstl.version}</version>
                    </dependency>


                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-jdbc</artifactId>
                        <version>${spring.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context-support</artifactId>
                        <version>${spring.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-web</artifactId>
                        <version>${spring.security.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-config</artifactId>
                        <version>${spring.security.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                        <version>${spring.version}</version>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <version>${spring.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>${spring.version}</version>
                    </dependency>

                    <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring.version}</version>
                    </dependency>

                    <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${spring.version}</version>
                   </dependency>



                    <dependency>
                     <groupId>org.springframework</groupId>
                     <artifactId>spring-orm</artifactId>
                     <version>${spring.version}</version>
                    </dependency>


                     <dependency>
                            <groupId>org.hibernate</groupId>
                            <artifactId>hibernate-core</artifactId>
                            <version>4.1.9.Final</version>
                     </dependency> 

                 <dependency>
                        <groupId>javassist</groupId>
                        <artifactId>javassist</artifactId>
                        <version>3.12.1.GA</version>
                </dependency>

                <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-ehcache</artifactId>
                <version>4.2.6.Final</version>
                </dependency>



              </dependencies>
            </project>



Please help me here at the earliest. I got stuck on this error. 

Thanks
Ravi
mv.ravikumarece@gmail.com

2 个答案:

答案 0 :(得分:0)

从堆栈跟踪中可以清楚地看出,sessionFactory的类兼容性存在问题。

您正在使用Hibernate4,并且您的类属性标记包含hiberate 3的类引用。

将sessionFactory声明更改为:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
                <property name="dataSource" ref="dataSource" />
                <property name="configLocation">
                    <value>classpath:hibernate.cfg.xml</value>
                </property>
                ...........

答案 1 :(得分:0)

但是org.springframework.orm.hibernate3.LocalSessionFactory是spring-orm.jar的一部分吗?和春季版3.2.4。发布

你怎么说?