VFS含義是虛拟檔案系統;主要是通過程式能夠友善讀取本地檔案系統、FTP檔案系統等系統中的檔案資源。
Mybatis中提供了VFS這個配置,主要是通過該配置可以加載自定義的虛拟檔案系統應用程式。
一.配置應用
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="vfs" ref="vfs"/>
</bean>
<bean id="vfs" class="com.zzy.vfs.VFSmybatisTest">
<!-- 自定義虛拟檔案系統應用程式不做說明,後期單獨介紹 -->
</bean>
二.源碼分析
1.vfs在源碼中加載過程
if (this.vfs != null) {
configuration.setVfsImpl(this.vfs);
}
setVfsImpl(this.vfs) 加載自定義的vfs
2.setVfsImpl(this.vfs)方法源碼
public void setVfsImpl(Class<? extends VFS> vfsImpl) {
if (vfsImpl != null) {
this.vfsImpl = vfsImpl;
VFS.addImplClass(this.vfsImpl);
}
}
其中vfsImpl是vfs的實作。
3.VFS相關class所在的包

4.VFS源碼
public abstract class VFS {
源碼可自己檢視,這裡簡單介紹下VFS其中的源碼内容
}
VFS中有piblic method和protected method
piblic method:
A.把自定義的類添加到VFS實作list中
public static void addImplClass(Class<? extends VFS> clazz) {
if (clazz != null) {
USER_IMPLEMENTATIONS.add(clazz);
}
}
B.獲得VFS的單例
2.public static VFS getInstance() {
}
C.VFS的實作在目前環境下有效時傳回true
public abstract boolean isValid();
D.遞歸列出所有的資源
public List<String> list(String path) throws IOException {
List<String> names = new ArrayList<String>();
for (URL url : getResources(path)) {
names.addAll(list(url, path));
}
return names;
}
protected method:
A.根據類名擷取class
protected static Class<?> getClass(String className)
B.擷取類中的方法
protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
C.執行對象的方法
protected static <T> T invoke(Method method, Object object, Object... parameters)
D.擷取資源list
protected static List<URL> getResources(String path) throws IOException
E.遞歸列出所有資源
protected abstract List<String> list(URL url, String forPath) throws IOException;
類中定義了一個常量數組
public static final Class<?>[] IMPLEMENTATIONS = { JBoss6VFS.class, DefaultVFS.class };
IMPLEMENTATIONS 該數組中有兩個VFS :JBoss6VFS和DefaultVFS
5.JBoss6VFS:JBoss6提供的vfs api
public class JBoss6VFS extends VFS
查找通路JBoss 6 VFS需要的所有class和method,其他方法比較簡單,不做詳細介紹。
6.DefaultVFS api:預設實作
可以讀取大部分服務的系統檔案資源,大部分情況使用DefaultVFS 足夠。
A.Public中的isValid和list是對父類的Override
B.Protected的方法
listResources(JarInputStream jar, String path)
列出所有的 JarInputStream
findJarForResource(URL url)
通過給定url找到jar檔案中包含的資源
getPackagePath(String packageName)
Java包名轉換成path
isJar(URL url)
isJar(URL url, byte[] buffer)
是否是一個JAR file
歡迎大家加入Java進階架構/網際網路(嚴禁教育訓練機構、廣告群,最幹淨的技術交流群):微信号:1083683150(備注:技術群或者4000G架構師資源)
微信平台本人收集個大量資源(4000G架構師資源,1000G大資料資源),隻做分享,歡迎大家關注擷取,保證免費,非任何機構
更多精彩請掃碼關注微信公衆号—— 名稱:java版web項目 id :java_project