java - 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。
非常感谢任何帮助。
解决方案
看起来您在根类加载器和 webapp 的 WAR 文件中都有 slf4j jar 文件。尝试使用 maven 将其标记为“提供”,因此它不会进入 WAR。这样 java 就不会对在多个类加载器中加载相同的类感到困惑。
推荐阅读
- r - 如何使用 R 中的光栅包将 RGB 图像转换为 CIELAB?
- python - 如何在调试模式下运行fastapi项目在vscode中自动重新加载
- python - 从行中删除任何 0 值,对行中的值进行降序排列,对于行中的每个非 0 值,将索引、列名和分数返回到新的 df
- python - 使用 bs4 和 Python 进行 URL 分离
- symfony - 如何解决“没有扩展能够加载'monolog.http_client'的配置”
- uml - plantuml - 活动图:具有三个传出边和 [else] 保护的决策节点
- html - Flexbox 布局在实际设备上默认反转
- go - 如何从 GORM 结构模型生成 SQL 代码?
- javascript - TypeError:store.getState 不是函数。(在'store.getState()'中,'store.getState'未定义我该如何解决这个问题?
- php - parse_str 只返回第一个参数