標籤: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的坑