Spring Hibernate配置问题

时间:2017-01-29 09:52:43

标签: mysql spring hibernate maven tomcat

当我启动我的tomcat服务器时,我没有得到JDBC驱动程序发现异常,所以为了解决这个问题,我在tomcat lib文件中放置了mysql-connector-java-5.1.40-bin.jar,之后我得到了以下异常: 嵌套异常是java.lang.NoClassDefFoundError:org / dom4j / DocumentException

在类路径资源[com / websystique / springmvc / configuration / HibernateConfiguration.class]中定义的'sessionFactory':init方法的调用失败;嵌套异常是java.lang.NoClassDefFoundError:org / dom4j / DocumentException

这是我的pom.xml

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

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.websystique.springmvc</groupId>
    <artifactId>SpringMVCHibernateManyToManyCRUDExample</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>SpringMVCHibernateWithSpringSecurityExample</name>

    <properties>
        <springframework.version>4.2.5.RELEASE</springframework.version>
        <springsecurity.version>4.0.4.RELEASE</springsecurity.version>
        <hibernate.version>4.3.11.Final</hibernate.version>
        <mysql.connector.version>5.1.31</mysql.connector.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>


        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- jsr303 validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>

        <!-- MySQL -->
        <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>
        <!-- SLF4J/Logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <warName>SpringMVCHibernateWithSpringSecurityExample</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <finalName>SpringMVCHibernateWithSpringSecurityExample</finalName>
    </build>
</project>

dom4j在构建路径中可用

dom4j is available in build path

3 个答案:

答案 0 :(得分:1)

我认为你的项目依赖项或tomcat的库文件夹中可能有另一个版本的dom4j。您可以尝试添加范围&#34;提供&#34;对于你的pom.xml中的dom4j

<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>provided</scope>

如果它有效,则意味着你的tomcat中还有另一个版本的dom4j。如果它不起作用,您应该再次仔细检查您的maven依赖关系树。

答案 1 :(得分:1)

<dependency>自动删除dom4j 1.6.1hibernate-core的依赖关系,这样您无论如何都会在类路径中拥有dom4j.jar

MySQL不是tomcat要求的依赖项,为什么要把它放在那里?! tomcat的lib文件夹不是mysql.jar创建endorsed - 文件夹的正确位置,请将其放在那里。

最好更换

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

具有以下属性:

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>

能够通过mvn -Dmaven.compiler.target=1.8重载。

问题

与许多容器架构(如tomcats web-container)一样,不同容器有不同的类加载器。您将hibernate-core-xx.jar放在了tomcat的lib - 文件夹中。所以这些类被加载到一个没有dom4j可用的低级类加载器中。是的,您的应用程序包含dom4j和hibernate-core-xx.jar,但由于hibernate-core-xx.jar已经由tomcat加载,因此它不会再次加载,并且tomcat的类加载器无法访问您的应用程序类加载器。所以,从tomcat中删除所有额外的jar,并且只支持mysql-xxx.jar。

答案 2 :(得分:0)

确保dom4j.jar在类路径中。

main