- 目录
- 概 述
- 小结
- 参考资料和推荐阅读
目录 概 述LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code and KPI, Keep progress,make a better result.
Survive during the day and develop at night。
在 Mybatis 中,与磁盘文件的交互主要是对 xml 配置文件的读取操作。除了对磁盘的读写,还有对内存中类文件的操作。
ClassLoaderWrapper 和 Resources
源码如下:
public class ClassLoaderWrapper {
ClassLoader defaultClassLoader;
ClassLoader systemClassLoader;
ClassLoaderWrapper() {
try {
systemClassLoader = ClassLoader.getSystemClassLoader();
} catch (SecurityException ignored) {
// AccessControlException on Google App Engine
}
}
public URL getResourceAsURL(String resource) {
return getResourceAsURL(resource, getClassLoaders(null));
}
public URL getResourceAsURL(String resource, ClassLoader classLoader) {
return getResourceAsURL(resource, getClassLoaders(classLoader));
}
public InputStream getResourceAsStream(String resource) {
return getResourceAsStream(resource, getClassLoaders(null));
}
public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
return getResourceAsStream(resource, getClassLoaders(classLoader));
}
测试代码:
@Test
void getResourceAsURL() {
assertNotNull(wrapper.getResourceAsURL(JPETSTORE_PROPERTIES));
}
@Test
void getResourceAsURLNotFound() {
assertNull(wrapper.getResourceAsURL(RESOURCE_NOT_FOUND));
}
测试结果:
Resources:
源码如下:
public class Resources {
private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
private static Charset charset;
Resources() {
}
public static ClassLoader getDefaultClassLoader() {
return classLoaderWrapper.defaultClassLoader;
}
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
classLoaderWrapper.defaultClassLoader = defaultClassLoader;
}
public static URL getResourceURL(String resource) throws IOException {
// issue #625
return getResourceURL(null, resource);
}
public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
if (url == null) {
throw new IOException("Could not find resource " + resource);
}
return url;
}
public static InputStream getResourceAsStream(String resource) throws IOException {
return getResourceAsStream(null, resource);
}
public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) {
throw new IOException("Could not find resource " + resource);
}
return in;
}
public static Properties getResourceAsProperties(String resource) throws IOException {
Properties props = new Properties();
try (InputStream in = getResourceAsStream(resource)) {
props.load(in);
}
return props;
}
public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
Properties props = new Properties();
try (InputStream in = getResourceAsStream(loader, resource)) {
props.load(in);
}
return props;
}
测试代码:
@Test
void shouldGetUrlForResource() throws Exception {
URL url = Resources.getResourceURL(JPETSTORE_PROPERTIES);
assertTrue(url.toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
}
ResolveUtil 和 VFS 及其实现类:
static VFS createVFS() {
// Try the user implementations first, then the built-ins
List> impls = new ArrayList<>();
impls.addAll(USER_IMPLEMENTATIONS);
impls.addAll(Arrays.asList((Class extends VFS>[]) IMPLEMENTATIONS));
// Try each implementation class until a valid one is found
VFS vfs = null;
for (int i = 0; vfs == null || !vfs.isValid(); i++) {
Class extends VFS> impl = impls.get(i);
try {
vfs = impl.getDeclaredConstructor().newInstance();
if (!vfs.isValid() && log.isDebugEnabled()) {
log.debug("VFS implementation " + impl.getName()
+ " is not valid in this environment.");
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
log.error("Failed to instantiate " + impl, e);
return null;
}
}
if (log.isDebugEnabled()) {
log.debug("Using VFS adapter " + vfs.getClass().getName());
}
return vfs;
}
}
ResolveUtil 作为一个工具类,主要用于遍历某一个路径下所有的文件,并且找到符合条件的文件,这是两个功能 遍历、筛选。
我们做下测试:
@Test
void invoke() throws IOException, NoSuchMethodException {
VFS vfs = VFS.invoke(VFS.class.getMethod("getInstance"), VFS.class);
Assertions.assertEquals(vfs, VFS.getInstance());
Assertions.assertThrows(RuntimeException.class, () -> {
//java.lang.IllegalArgumentException: wrong number of arguments
VFS.invoke(VFS.class.getMethod("getInstance"), VFS.class, "unnecessaryArgument");
});
Assertions.assertThrows(IOException.class, () -> {
//InvocationTargetException.getTargetException -> IOException
VFS.invoke(Resources.class.getMethod("getResourceAsProperties", String.class), Resources.class, "invalidResource");
});
Assertions.assertThrows(RuntimeException.class, () -> {
//Other InvocationTargetException
VFS.invoke(Integer.class.getMethod("valueOf", String.class), Resources.class, "InvalidIntegerNumber");
});
}
小结
参考资料和推荐阅读
1.链接: link.



