AppClassLoader和WebAppClasssLoader的坑

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   java   for   sp   檔案   

最近,打算學習一下spring mvc,為後續做一些積累。

 

搭建spring+mybatis,動態建立mapper,mapper的檔案名稱和類在一個目錄,但是我之前犯個

錯誤,大小寫寫錯了,結果我用普通的java程式運行,沒有問題,結果部署到tomcat下面,總是

報statement無法找到,奇怪了,java程式可以找到啊。

 

可以肯定的是兩個程式classloader不一樣,一個是system的,一個是tomcat的。

深入一下找到了mybatis代碼,如下:

private void loadXmlResource() {        // Spring may not know the real resource name so we check a flag        // to prevent loading again a resource twice        // this flag is set at XMLMapperBuilder#bindMapperForNamespace        if (!configuration.isResourceLoaded("namespace:" + type.getName())) {            String xmlResource = type.getName().replace(‘.‘, ‘/‘) + ".xml";            InputStream inputStream = null;            try {                System.out.println("ClassLoader: " + type.getClassLoader());                inputStream = Resources.getResourceAsStream(                    type.getClassLoader(), xmlResource);            } catch (IOException e) {                // ignore, resource is not required            }            if (inputStream != null) {                XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream,                    assistant.getConfiguration(), xmlResource,                    configuration.getSqlFragments(), type.getName());                xmlParser.parse();            }        }    }

 奇怪,就是用classloader載入麼,按理說沒有問題,都是類路徑下面的。

 

找的是類名稱對應的xml,按理說兩個行為應該一樣啊,都是找不到,不能一個找到,一個沒找到。

後來想起來,在windows平台檔案大小寫不區分吧,結果果斷的把名字改過來,結果正常了。

 

我去了,可能是我知識理解的少,這也太坑了。雖然配置錯了是我的問題。。。。。

AppClassLoader和WebAppClasssLoader的坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.