简体   繁体   中英

java.lang.LinkageError: loader constraint violation for Spring MVC and Thymeleaf

I am building my application using Spring MVC and Thymeleaf. I am also using Log4j for Logging. While invoking one of the service where the Thymeleaf Template is getting loaded I get the following Error:

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

This error is thrown when the following line of code is executed:

ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();

If I invoke the same Service Again(without making any changes or restart/clean of Server) I am getting a different Error:

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

The gradle config file is:

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

Server used - Tomcat.

Any help is highly appreciated.

It would seem that you have the slf4j jar file in both the root class loader, and your webapp's WAR file. Try using maven to mark it as 'provided', so it doesn't get in the WAR. Then java won't get confused about the same classes being loaded in multiple class loaders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM