LinkageError - 加载器约束违规 - 解析方法时“org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()”

时间:2017-02-16 13:02:25

标签: java tomcat maven-2 classloader slf4j

我收到以下错误:

ContextListenerjava.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader 'org.apache.catalina.loader.WebappClassLoader@4d1cc3c3 (urls: ['], parents: [)'java.net.URLClassLoader@4fccd51b']) of the current class, org/slf4j/LoggerFactory, and the class loader 'System (urls: [''file:/usr/lib/jvm/jvm_8/jvm_8/lib/jvmx.jar', 'file:/usr/lib/jvm/jvm_8/jvm_8/lib/tools.jar', 'file:/ ....

我的云平台默认包slf4j,我收到此错误,因为我也将slf4j作为依赖项放在我的pom文件中,两个不同的类加载器正在加载slf4j

我解决问题的方法是将pom中的依赖范围更改为提供。

但副作用是我无法在我的本地tomcat上运行它,因为它找不到slf4j库。

在这种情况下可以做些什么,以便我的战争在我的云平台和本地tomcat上运行良好?

2 个答案:

答案 0 :(得分:0)

您可以在pom.xml中使用不同的配置文件。

SAP Cloud Platform示例:

<profiles>
        <profile>
            <id>local</id>
            <properties>
                <packaging.type>jar</packaging.type>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                </dependency>
            </dependencies>
        </profile>
    <profile>
        <id>scp</id>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <build>
            <finalName>LoraConnector</finalName>
        </build>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

您可以使用&#34;运行配置&#34;在Eclipse(或其他工具)中或当您构建war文件时:

mvn -P scp clean package

答案 1 :(得分:0)

如果您使用的是spring-boot-starter-web-1.5.1.RELEASE.jar,那么您需要排除jul-to-slf4j-1.7.22.jar,jcl-over-slf4j-1.7.22。这两个罐子有冲突slf4j-api。 <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <packagingExcludes> WEB-INF/lib/jcl-over-slf4j-1.7.22.jar,WEB-INF/lib/jul-to-slf4j-1.7.22.jar </packagingExcludes> </configuration> </plugin>