具有非嵌入式Tomcat的Spring Boot:加载器约束违规错误

时间:2014-10-29 16:28:22

标签: spring jsp spring-mvc spring-boot

尝试使用Spring Boot显示JSP时出错。这是我的配置:

  • Spring source STS(最新版本)
  • Spring Boot 1.1.8(迄今为止的最新版本)
  • Java 1.7
  • Tomcat 8.0.14(我没有使用嵌入式tomcat,所以我手动启动了弹簧启动应用程序)

我的JSP文件是准系统:

TEST

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.edhec</groupId>
<artifactId>stdapps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>stdapps</name>
<description>Project stdapps</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.4.0</version>
    </dependency>
    <!-- CAS Client -->
    <dependency>
       <groupId>org.jasig.cas.client</groupId>
       <artifactId>cas-client-core</artifactId>
       <version>3.3.3</version>
       <scope>runtime</scope>
       <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>

</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.edhec.stdapps.Application</start-class>
    <java.version>1.7</java.version>
    <tomcat.version>8.0.14</tomcat.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这是我得到的错误:

java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/WEB_002dINF/views/agreement2/index_jsp, and the class loader (instance of java/net/URLClassLoader) for resolved class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type .getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager; used in the signature

我认为它与el或jsp-api版本有关系,我尝试在pom.xml中手动添加,但是在我编译的jsp上有相同的错误或有时'noClassDefFoundError'

有人看到出了什么问题吗?

2 个答案:

答案 0 :(得分:18)

由于您依赖spring-boot-starter-webspring-boot-starter-websocket及其传递依赖关系,因此您需要在war文件中打包Tomcat。这导致有两种不同版本的Tomcat类可用,并导致约束违规。您需要将Tomcat依赖项标记为已提供,以便它们不会打包在WEB-INF/lib中。

将以下依赖项添加到您的pom:

<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <scope>provided</scope>
</dependency>

最好将依赖关系标记为提供,而不是将它们完全排除,因为它允许将war文件部署到Tomcat或使用java -jar执行。这是有效的,因为提供的依赖项打包在WEB-INF/lib-provided中,它们将被独立的Servlet容器忽略,但在您使用嵌入式容器时可用。

答案 1 :(得分:0)

在gradle中,我将此行添加到依赖项中:

dependencies {
   ...
   providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
   ...
}