【死磕 Spring】----- IOC 之 Spring 统一资源加载策略

 原文出自:

从上图可以看到,Resource 根据资源的不同类型提供不同的具体实现,如下:

AbstractResource 为 Resource 接口的默认实现,它实现了 Resource 接口的大部分的公共实现,作为 Resource 接口中的重中之重,其定义如下:

public abstract class AbstractResource implements Resource {      /**      * 判断文件是否存在,若判断过程产生异常(因为会调用SecurityManager来判断),就关闭对应的流      */     @Override     public boolean exists() {         try {             return getFile().exists();         }         catch (IOException ex) {             // Fall back to stream existence: can we open the stream?             try {                 InputStream is = getInputStream();                 is.close();                 return true;             }             catch (Throwable isEx) {                 return false;             }         }     }      /**      * 直接返回true,表示可读      */     @Override     public boolean isReadable() {         return true;     }      /**      * 直接返回 false,表示未被打开      */     @Override     public boolean isOpen() {         return false;     }      /**      *  直接返回false,表示不为 File      */     @Override     public boolean isFile() {         return false;     }      /**      * 抛出 FileNotFoundException 异常,交给子类实现      */     @Override     public URL getURL() throws IOException {         throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");     }      /**      * 基于 getURL() 返回的 URL 构建 URI      */     @Override     public URI getURI() throws IOException {         URL url = getURL();         try {             return ResourceUtils.toURI(url);         }         catch (URISyntaxException ex) {             throw new NestedIOException("Invalid URI [" + url + "]", ex);         }     }      /**      * 抛出 FileNotFoundException 异常,交给子类实现      */     @Override     public File getFile() throws IOException {         throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");     }      /**      * 根据 getInputStream() 的返回结果构建 ReadableByteChannel      */     @Override     public ReadableByteChannel readableChannel() throws IOException {         return Channels.newChannel(getInputStream());     }      /**      * 获取资源的长度      *      * 这个资源内容长度实际就是资源的字节长度,通过全部读取一遍来判断      */     @Override     public long contentLength() throws IOException {         InputStream is = getInputStream();         try {             
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信