使用controller修改properties配置文件

本文介绍了在Spring Boot中如何动态地通过Controller修改properties配置文件,以适应远程服务地址变化的需求。文中详细讨论了配置文件的读取、实体类、Service及Controller的实现,并分析了在静态变量中使用@Value注解的问题,以及打成jar包后可能出现的资源读取异常。文章还提到了Java文件读写的基础知识和转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求说明

在更改之前,远程服务的地址是硬编码的。由于远程服务的地址有时候会变化,手动更改,打包效率低下,所以产生了用一个配置文件来维护远程服务的地址,然后将其读取到变量当中。
印象深刻的就是Jar包内部文件的读取得流式读取,同时用classes下的相对路径写入

程序部分

配置文件service.properties

service.url=https://localhost:8081/

配置的实体类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("service.url")
public class PlatformConfig {
    @Value("${service.url}")
    private String platFormUrl;

    public void setPlatformUrl(String platFormUrl){
        this.platFormUrl=platFormUrl;

    }
    public String getPlatFormUrl(){
        return platFormUrl;
    }
}

提供修改服务的Service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.Map;
import java.util.Properties;

@Service
public class ConfigService {
    @Autowired
    private PlatformConfig platFormConfig;

    public void UpdateConfig(Map<String,String> configParames)throws Exception{
        String filepath = "/service.properties";
        InputStream ins = null;
        Properties props = new Properties();
        BufferedWriter bw = null;
        try {
            ins = this.getClass().getResourceAsStream("/service.properties");
            props.load(ins);
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath)));
            for (String key : configParames.keySet()) {
                props.setProperty(key, configParames.get(key));
            }
            platFormConfig.setPlatformUrl(props.getProperty("service.url"));
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println(filepath + " updates " + "" + " value error");
        } finally {
            try {
                if (bw!=null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

外部调用的controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class ConfigController {
	@Autowired
	private PlatformConfig platformConfig;
	@Autowired
	private ConfigService configService;
	@RequestMapping(value="/getUrl")
	public String getUrl(){
		return "the current platform url is"+platformConfig.getPlatFormUrl();
	}

	@RequestMapping(value="/updateUrl",method = RequestMethod.POST)
	public JSONObject updateProperties(@RequestBody Map<String, String> keyValueMap) throws Exception {
		JSONObject result = new JSONObject();
		configService.UpdateConfig(keyValueMap);
		result.put("currentPlatUrl",platformConfig.getPlatFormUrl());
		return result;
	}
}

问题总结

classes底下的配置文件会发生修改
为什么静态变量不能用注解@Value进行赋值呢(静态变量注解赋值完为null)
http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field
http://stackoverflow.com/questions/6897201/an-alternative-to-value-annotation-in-static-function

打完jar包后发生了nullException 异常
当打成一个jar包后,整个jar包是一个文件,只能使用流的方式读取资源,这时候就不能通过File来操作资源了。在IDE中之所以能正常运行,是因为IDE中的资源文件在target/classes目录下,是正常的文件系统结构。用classes目录下文件的相对路径也可以找到文件并用bufferwriter写入。

求助贴,修改properties,内存中是改了,但是文件没有变化
InputStream,String,File相互转化
Java项目打成jar包后,如何获取项目中文件的路径
读取property配置文件生成静态变量

暴露的知识点不足

Java 文件读取写入的几个常见类之间的转化关系以及基础不牢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值