最近用spring boot 开发微服务,应该最后都是打成jar包,配置文件在jar包里面,要修改配置文件不太好修改,我就弄了个页面对配置文件内容进行修改。
简要介绍一下流程:
1.将配置文件内容取出来,存放起来
2.写一个controller层供页面调用
3.将内容在页面展示出来
4.将修改内容写回配置文件
具体流程
1.假设我们已经建好了一个spring boot工程。
2.写一个对properties操作的公共类,获取properties里面所有内容
public static List
3.写一个可以被访问的controller,提供请求路径,返回所有内容
@RequestMapping(value = "/loadAllProperty",method = RequestMethod.GET) public BaseResult loadAllProperty(){ BaseResult result = new BaseResult(); try{ List
> propertyList = PropertyUtil.loadAllProperty(); result.setCode(Constants.SUCCESS); result.setObj(propertyList); }catch(Exception e){ e.printStackTrace(); } return result; } 4.写个页面,将内容展示出来页面:
key value {{property.key}} js:<script> var myApp=angular.module('myApp',[]); myApp.controller('myController',function($scope,$http){ $scope.init=function(){ $http({ url:'./loadAllProperty', method:'GET' }).then(function(result){ /* data,header,config,status*/ if(result.data.code==200){ $scope.propertyList = result.data.obj; }else{ console.log(result.data.code); } }).catch(function(result){ console.log("error"); }); } $scope.changeProperty=function(){ $http({ url:'./changeProperty', method:'POST', data:{ list:$scope.propertyList } }).then(function(result){ /* data,header,config,status*/ if(result.data.code==200){ $scope.init(); }else{ console.log(result.data.code); } }).catch(function(result){ console.log("error"); }); } $scope.init(); }); </script>
5.在写一个请求,接受修改后peoprerties@RequestMapping(value = "/changeProperty",method = RequestMethod.POST) @ResponseBody public BaseResult changeProperty(@RequestBody Map map){ BaseResult result = new BaseResult(); try{ PropertyUtil.changeProperty((List
>) map.get("list")); result.setCode(Constants.SUCCESS); }catch(Exception e){ e.printStackTrace(); } return result; } 6.在properties工具类里面在添加一个将内容写入到properties里面的方法public static void changeProperty(List
> propertyList){ try{ if(propertyList!=null&&propertyList.size()!=0){ for(Map propertyMap:propertyList){ PROPERTY.setProperty(String.valueOf(propertyMap.get("key")),String.valueOf(propertyMap.get("value"))); } } OutputStream ops = new FileOutputStream(PropertyUtil.class.getResource(CONFIGPATH).getPath()); PROPERTY.store(ops,null); ops.close(); }catch(Exception e){ e.printStackTrace(); } } 注意:在调用之后很多会去src下看properties,发现文件没有被修改,其实修改的在classes下面。