修改微服务中配置文件


最近用spring boot 开发微服务,应该最后都是打成jar包,配置文件在jar包里面,要修改配置文件不太好修改,我就弄了个页面对配置文件内容进行修改。

         简要介绍一下流程:

         1.将配置文件内容取出来,存放起来

         2.写一个controller层供页面调用

         3.将内容在页面展示出来

         4.将修改内容写回配置文件

         具体流程

         1.假设我们已经建好了一个spring boot工程。

     2.写一个对properties操作的公共类,获取properties里面所有内容

 public static List
    
    
     
     > loadAllProperty(){
        /*构建返回值*/
        List
     
     
      
      
       
       > propertyList = new ArrayList
       
        
        
          >(); /*将properties里面值都取出来并且遍历*/ Iterator propertyIterator = PROPERTY.entrySet().iterator(); if(propertyIterator.hasNext()){ Map.Entry propertyEntry = (Map.Entry)propertyIterator.next(); Map 
         
           propertyMap = new HashMap 
          
            (); propertyMap.put("key",propertyEntry.getKey()); propertyMap.put("value",propertyEntry.getValue()); propertyList.add(propertyMap); } return propertyList; } 
           
          
        
      
      
    
    

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.写个页面,将内容展示出来
页面:

     
     
keyvalue
{{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下面。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值