引言: 诡异而少见的java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayout异常错误信息,在启动的时候,出现了异常,哪里出了问题呢?
1. 背景介绍
项目是Web项目,使用了slf4j-api, 使用logback做为默认的日志包;之前一直工作正常,在将日志路径调整到了pom.xml之后,忽然就蹦出来一个异常错误信息。
pom.xml中引用的maven片段:
<dependency> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> <version>1.7.10</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
2. 问题的出现
在某一天,忽然出现了以下问题,
17:03:02,893 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Fri Apr 24 17:03:02 CST 2015 17:03:02,893 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [ch.qos.lorgback.classic.PatternLayout] java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayout at java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayout at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) at at ch.qos.logback.core.util.Loader.loadClass(Loader.java:125) at at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:100) at at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:275) at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:147) at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:129) at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49) at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:77) at at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:152) at at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85) at at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55) at at org.slf4j.LoggerFactory.bind(LoggerFactory.java:142) at at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:121) at at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:332) at at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:284) at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156) at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132) at at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:274) at at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:282) at at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) at at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at at java.util.concurrent.FutureTask.run(FutureTask.java:138) at at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at at java.lang.Thread.run(Thread.java:662) 17:03:02,893 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@49:22 - no applicable action for [pattern], current ElementPath is [[configuration][appender][layout][pattern]] 17:03:02,903 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ThirdPartyAppender] - Active log file name: /opt/app/outpay/logs/es-thirdparty.log.2015-04-24.log 17:03:02,903 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ThirdPartyAppender] - File property is set to [null] 17:03:02,903 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[ThirdPartyAppender] - Encoder has not been set. Cannot invoke its init method.
从日志上看,在Spring web context启动的过程中,需要查找默认的日志框架,结果在最终的结果中,没有找到期望的类ch.qos.lorgback.classic.PatternLayout。经过确认,这个类确实是在我们系统的类库之中,但是为什么却报没有找到的异常呢?
3. 问题的分析与解决
为什么在Spring web context的加载中报错,但是类确实是存在于系统中呢?最终的解决是加载的顺序,就是在启动web context的类库之时,类库确实无法被访问到。
回想到pom.xml中加载的类库,确实是可以被限制在不同的访问scope中的,即scope限定了在不同的时候,应用所可以访问的类库包。
maven依赖关系中Scope的作用
在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值:
* compile,缺省值,适用于所有阶段,会随着项目一起发布。
* provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
* runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
* test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
* system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
此时我们的系统中使用了默认值compile. 故在web容器启动的时候,无法访问到可用的类库logback.jar。 按照通常的道理和分析而言,其是适用于所有阶段的,但这里为什么不可用,无法得知。
但是将其scope设置为provided, 则强制指定其为容器所用,故可以解决当前的异常问题。
正确的设置为:
<!-- Logger --> <dependency> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> <version>1.7.10</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> <scope>provided</scope> </dependency>
4. 总结
该问题的症结点在于正确的使用scope的作用域。
Web项目中诡异的java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayout问题解决
原文地址:http://blog.csdn.net/blueheart20/article/details/45248917