需求说明
在更改之前,远程服务的地址是硬编码的。由于远程服务的地址有时候会变化,手动更改,打包效率低下,所以产生了用一个配置文件来维护远程服务的地址,然后将其读取到变量当中。
印象深刻的就是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 文件读取写入的几个常见类之间的转化关系以及基础不牢。