云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > spring源码分析之resource

spring源码分析之resource

  • 62 次阅读
  • 3 次下载
  • 2025/5/4 16:03:18

Resource相关类图

看代码过程中觉得很好的地方

AbstractResource.java

publicbooleanexists() {

// Try file existence: can we find the file in the file system? try {

return getFile().exists();

}

catch (IOException ex) {

// Fall back to stream existence: can we open the stream? try {

InputStream is = getInputStream(); is.close(); returntrue;

}

catch (Throwable isEx) { returnfalse; } } }

这段代码,面向接口编程的典范,getInputStream()是父接口定义的方法,实际上的实现是在AbstractResource的子类中实现。以一种类似于模板方法模式的方式去编程,未来可扩展性较高。

AbstractFileResolvingResource.java

/**

* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime. */

privatestaticclassVfsResourceDelegate{

publicstatic Resource getResource(URL url) throws IOException { returnnew VfsResource(VfsUtils.getRoot(url)); }

publicstatic Resource getResource(URI uri) throws IOException {

returnnew VfsResource(VfsUtils.getRoot(uri)); } }

看上面的代码的注释,目的是防止在运行时对JBoss VFS API的强依赖。说白了还是要反向依赖,依赖注入。单独看这段代码是看不出如何做到的,仔细看VfsUtils里面的代码,如下:

protectedstatic Object getRoot(URL url) throws IOException {

return invokeVfsMethod(VFS_METHOD_GET_ROOT_URL, null, url); }

// 反射调用方法

protectedstatic Object invokeVfsMethod(Method method, Object target, Object... args) throws IOException { try {

return method.invoke(target, args); }

catch (InvocationTargetException ex) {

Throwable targetEx = ex.getTargetException(); if (targetEx instanceof IOException) { throw (IOException) targetEx; }

ReflectionUtils.handleInvocationTargetException(ex); }

catch (Exception ex) {

ReflectionUtils.handleReflectionException(ex); }

thrownew IllegalStateException(\code path reached\); }

invokeVfsMethod里的method的定义如下:

// 定义静态成员变量method

privatestatic Method VFS_METHOD_GET_ROOT_URL = null; privatestatic Method VFS_METHOD_GET_ROOT_URI = null; privatestaticfinal String VFS3_PKG = \; privatestaticfinal String VFS_NAME = \;

// 通过classLoader获取类

搜索更多关于: spring源码分析之resource 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

Resource相关类图 看代码过程中觉得很好的地方 AbstractResource.java publicbooleanexists() { // Try file existence: can we find the file in the file system? try { return getFile().exists(); } catch (IOException ex) { // Fall back to stream existence: can we open the stream? try { InputStream is = getInputStream();

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com