Web項目中詭異的java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayout問題解決

來源:互聯網
上載者:User

標籤:java   maven   scope   provided   

引言: 詭異而少見的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 201517: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.PatternLayoutat java.lang.ClassNotFoundException: ch.qos.lorgback.classic.PatternLayoutat 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 [email protected]: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.log17: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問題解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.