首页 > 解决方案 > java.lang.LinkageError:Spring MVC 和 Thymeleaf 的加载程序约束冲突

问题描述

我正在使用 Spring MVC 和 Thymeleaf 构建我的应用程序。我也在使用 Log4j 进行日志记录。在调用加载 Thymeleaf 模板的服务之一时,我收到以下错误:

Handler dispatch failed; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of org/apache/catalina/loader/ParallelWebappClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature

执行以下代码行时会引发此错误:

ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

如果我再次调用相同的服务(不进行任何更改或重新启动/清理服务器),我会收到不同的错误:

 Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.thymeleaf.templateresolver.ClassLoaderTemplateResolver

gradle 配置文件是:

compile group: 'org.thymeleaf', name: 'thymeleaf', version: '3.0.11.RELEASE'
compile group: 'org.thymeleaf', name: 'thymeleaf-spring4', version: '3.0.11.RELEASE'

使用的服务器 - Tomcat。

非常感谢任何帮助。

标签: javaspringspring-mvcthymeleaf

解决方案


看起来您在根类加载器和 webapp 的 WAR 文件中都有 slf4j jar 文件。尝试使用 maven 将其标记为“提供”,因此它不会进入 WAR。这样 java 就不会对在多个类加载器中加载相同的类感到困惑。


推荐阅读