美文网首页
SpringBoot打包后无法读取到resource目录下的co

SpringBoot打包后无法读取到resource目录下的co

作者: 你家门口的两朵云 | 来源:发表于2023-07-30 18:19 被阅读0次

    在封装fastdfs客户端的时候,发现在本地运行能够获取到配置文件的路径

     /**
         * 初始化客户端
         * ClientGlobal.init(filePath) 读取并初始化客户端
         */
        static {
            try {
                String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
                logger.info("初始化filePath{}",filePath);
                ClientGlobal.init(filePath);
            } catch (Exception e) {
                logger.error("初始化FastDFS失败", e.getMessage());
            }
        }
    

    结果:


    打包前

    打成jar包之后就无法获取文件了,原因是resource目录会打包到jar包的目录,我们上传文件到服务器,肯定不会读取这个绝对路径,而是读取打包后resource下的文件,所以绝对路径就行不通了,找不到文件就是情理之中的了。
    查阅资料后发现,打包后是一个jar包,无法直接读取jar包中的文件,读取只能通过类加载器读取。类加载器读取jar包中编译后的class文件,自然就能读取到我们想要的conf文件。解压jar包后查看目录结构,发现yml和conf都在class下:


    image.png

    然后我们可以根据yml读取的原理来读取这个配置文件即可
    翻阅springboot的run实现源码,找到读取方式

    image.png
    public ConfigurableApplicationContext run(String... args) {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            DefaultBootstrapContext bootstrapContext = createBootstrapContext();
            ConfigurableApplicationContext context = null;
            configureHeadlessProperty();
            SpringApplicationRunListeners listeners = getRunListeners(args);
            listeners.starting(bootstrapContext, this.mainApplicationClass);
            try {
                ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                //看到这行,初始化环境
                ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
                configureIgnoreBeanInfo(environment);
    
                        /*注释掉部分代码*/
    
            }
            catch (Throwable ex) {
                handleRunFailure(context, ex, listeners);
                throw new IllegalStateException(ex);
            }
            return context;
        }
    
        private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
                DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
            // Create and configure the environment
            ConfigurableEnvironment environment = getOrCreateEnvironment();
            configureEnvironment(environment, applicationArguments.getSourceArgs());
            ConfigurationPropertySources.attach(environment);
            listeners.environmentPrepared(bootstrapContext, environment);
            DefaultPropertiesPropertySource.moveToEnd(environment);
            configureAdditionalProfiles(environment);
            bindToSpringApplication(environment);
            if (!this.isCustomEnvironment) {
                environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
                        deduceEnvironmentClass());
            }
            ConfigurationPropertySources.attach(environment);
            return environment;
        }
    

    相关文章

      网友评论

          本文标题:SpringBoot打包后无法读取到resource目录下的co

          本文链接:https://www.haomeiwen.com/subject/mtlmhrtx.html