一、Nacos配置更新不生效

问题描述

在代码调适过程中,在Nacos中修改了文件上传的地址,但是在文件上传服务中读取出来的配置文件一直不会刷新,重启Nacos之后,修改之后的配置才生效

#文件上传配置
file:
  # 文件上传目录
  uploadFolder: E:/upload/

解决方法

我们需要在取此配置的代码处,加入注解@RefreshScope即可,如下:

package com.framework.pie.admin.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

@Configuration
@RefreshScope
public class UploadFileConfig {

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    public String getUploadFolder() {
        if (uploadFolder == null){
            File path = null;
            try {
                path = new File(ResourceUtils.getURL("classpath:").getPath());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            if(!path.exists()) path = new File("");{
               uploadFolder = path.getAbsolutePath();
            }
        }
        return uploadFolder;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容