zoukankan      html  css  js  c++  java
  • FileSystemResource在Srping FrameWork 5中的变化

    之前在项目中一直使用FileSystemResource这个类作为PropertyPlaceholderConfigurer的Resource引入部署目录外的配置文件,并设置了setIgnoreResourceNotFound为true,最近把Spring Framework升级为5.0.2,当外部配置文件不存在时,PropertyPlaceholderConfigurer的初始化会报错,貌似setIgnoreResourceNotFound不再起作用,今天看了一下源码

    先看PropertyPlaceholderConfigurer的父类PlaceholderConfigurerSupport中的一段:

    /**
    * Load properties into the given instance.
    * @param props the Properties instance to load into
    * @throws IOException in case of I/O errors
    * @see #setLocations
    */
    protected void loadProperties(Properties props) throws IOException {
        if (this.locations != null) {
            for (Resource location : this.locations) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Loading properties file from " + location);
                }
                try {
                    PropertiesLoaderUtils.fillProperties(
                            props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
                }
                catch (IOException ex) {
                    // Resource not found when trying to open it
                    if (this.ignoreResourceNotFound &&
                            (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Properties resource not found: " + ex.getMessage());
                        }
                    }
                    else {
                        throw ex;
                    }
                }
            }
        }
    }
    

    得知,在设置了ignoreResourceNotFound之后,Resource类抛出的异常必须是FileNotFoundException才能生效,但是在Spring Framework 5中FileSystemResource已经换用nio下面的包来实现了,抛出的也是nio包下面的异常java.nio.file.NoSuchFileException,所以ignoreResourceNotFound不再有效,从FileSystemResource的说明中得知还有PathResource这个类,就换它试了一下,问题解决。

  • 相关阅读:
    【随笔浅谈】splay 时间复杂度简要分析
    【Luogu P4406】「CQOI2005」三角形面积并
    LLVM12.0.1,编译
    electrion 为了便于调试,打开控制台
    MySQL插入大量数据探讨
    【Django前后端部署】更新部署,不使用反向代理
    检测两台服务器某个目录下的文件一致性
    ceph-rbd和cephfs使用
    Laravel
    Scrcpy投屏神器--让你的电脑流畅操作手机
  • 原文地址:https://www.cnblogs.com/cfrost/p/8324022.html
Copyright © 2011-2022 走看看