You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot运行时动态读写并更新application.properties参数的方法

如何在Spring Boot中每次请求时动态读写application.properties参数

嘿,你这个需求我之前帮好几个开发者解决过——Spring Boot默认的配置加载机制确实是静态的,启动后就固定了,但咱们有几种办法能实现每次请求时读写指定配置参数的效果,分两种场景给你说说:

内存级动态修改(无需持久化)

如果你的需求只是在应用运行期间临时修改参数,不需要把改动保存到配置文件里,这个方案最简单高效:

  1. 先创建一个配置绑定类,用@ConfigurationProperties映射你要操作的参数(比如我这里假设要操作的参数是app.custom.value):
@Component
@ConfigurationProperties(prefix = "app.custom")
public class CustomConfig {
    private String value;

    // 标准的getter和setter
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
  1. 然后在你的@RestController里注入这个类,每次请求时直接读写它的属性就行:
@RestController
@RequestMapping("/api/config")
public class ConfigController {

    private final CustomConfig customConfig;

    // 构造注入(推荐的Spring注入方式)
    public ConfigController(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    @GetMapping
    public String getCurrentValue() {
        // 读取当前配置值
        return "当前参数值:" + customConfig.getValue();
    }

    @PostMapping
    public String updateValue(@RequestParam String newValue) {
        // 读取旧值并设置新值
        String oldValue = customConfig.getValue();
        customConfig.setValue(newValue);
        return "参数值已从「" + oldValue + "」修改为「" + newValue + "」";
    }
}

注意:这个修改只存在于应用内存中,重启应用后会恢复成application.properties里的原始值。

持久化到application.properties文件

如果需要把修改后的参数值保存到配置文件里,重启后依然生效,那就要手动处理文件的读写了:

@RestController
@RequestMapping("/api/config/file")
public class ConfigFileController {

    @Autowired
    private ResourceLoader resourceLoader;

    @GetMapping
    public String getValueFromFile() throws IOException {
        // 加载classpath下的application.properties
        Resource resource = resourceLoader.getResource("classpath:application.properties");
        Properties props = new Properties();
        props.load(resource.getInputStream());
        return "配置文件中的当前值:" + props.getProperty("app.custom.value");
    }

    @PostMapping
    public String updateValueInFile(@RequestParam String newValue) throws IOException {
        Resource resource = resourceLoader.getResource("classpath:application.properties");
        File configFile = resource.getFile();

        // 读取现有配置
        Properties props = new Properties();
        props.load(new FileInputStream(configFile));
        String oldValue = props.getProperty("app.custom.value");

        // 设置新值并写入文件
        props.setProperty("app.custom.value", newValue);
        props.store(new FileOutputStream(configFile), "自动更新于:" + LocalDateTime.now());

        return "配置文件已更新:从「" + oldValue + "」修改为「" + newValue + "」";
    }
}

这里有个关键注意点:如果是生产环境把应用打包成Jar包,classpath下的配置文件是Jar内部的文件,无法直接修改。这时候你需要把application.properties放在Jar外部的目录,然后通过spring.config.location参数指定加载路径(比如启动时加--spring.config.location=file:/opt/myapp/application.properties),这样才能正常写入修改。

额外:让其他Bean感知配置变化

如果你的其他Bean也需要实时感知到这个参数的变化,可以结合@RefreshScope注解:

  1. 在你的CustomConfig类上加上@RefreshScope:
@Component
@ConfigurationProperties(prefix = "app.custom")
@RefreshScope
public class CustomConfig {
    // 内容不变
}
  1. 修改配置文件后,通过Spring Boot Actuator的/actuator/refresh端点触发配置刷新(需要先引入spring-boot-starter-actuator依赖,并在配置中开启refresh端点)。不过这个操作比较重,建议只在必要时使用。

内容的提问来源于stack exchange,提问作者Max Sandoval

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:37:53