zoukankan      html  css  js  c++  java
  • 给你的JAVA程序配置参数(Properties的使用)

    我们在写JAVA程序时,很多时候运行程序的参数是需要动态改变的

    测试时一系列参数,运行时一系列参数

    又或者数据库地址也需要配一套参数,以方便今后的动态部署

    这些变量的初始化,我们在写小DEMO时完全可以写死在JAVA文件中

    但程序需要发布或者局部部署时,这些参数就需要脱离程序代码了

    我们有多种存放参数的方式,比如数据库、XML文件又或者直接是txt文件

    现在介绍一种使用JAVA,简单方便的参数读取方式

    .properties文件,我们并不陌生,很多优秀的框架中就能看到它的存在,比如Hibernate

    在src文件目录下,新建一个后缀为.properties的文件,用任意文本编辑器打开它,就可以使用键值对的方式设置您程序的运行参数了

    类似于这样

    #wangqun fd60e46db0dc119cfea740c3375fd7c4
    #toAccountId=fd60e46db0dc119cfea740c3375fd7c4
    #huangshi 1645a78135328c4b
    toAccountId=1645a78135328c4b
    #tangwei 6c0f7514f4bd0016
    #shixiaoping 98f30bad9e6789af
    #toAccountId=6c0f7514f4bd0016
    #toAccountId=6c0f7514f4bd0016,1645a78135328c4b,98f30bad9e6789af
    
    #cron=0 32 8-11,14-20/1 * * ?
    cron=0/10 * * * * ?
    
    fileName=d\:/\u59DC\u5830\u5468\u79EF\u5206\u7BA1\u63A7\u53CA\u53CC\u767E\u5146\u8BAD\u7EC3\u84250830.xlsx
    sheetName=\u6C47\u603B
    fromIndex=0,1
    toIndex=48,12
    #fromIndex=1,4
    #toIndex=13,12
    
    dirPath=C:/apache-tomcat-8.0.28/webapps/test/
    urlPath=http://61.132.43.176:8081/test/
    
    database_host=132.240.9.36
    database_port=1521
    database_user=jy
    database_name=yxdb
    database_pwd=OVQwu8QSm4CWktEZdnjtxg==
    
    sql_str=select * from hs_bb_rbb r order by r.no
    modify_sheet=sheet1
    modify_from=1,0
    time_cell=1,4

    注意一下几点:

    一、用#号表示注释,可以多录入一些配置可能,运行时动态注释或者打开,比较方便

    二、.properties明文存储,所以敏感字符需要加密,比如数据的密码

    三、遇到中文,是个棘手的问题,最简单的方式是使用MyEclipse的properties文件编辑器来写入,会自动将中文转码

    四、所有参数只可以以字符串形式存储,至于类型转换,请在JAVA中解析完成

    五、适用于只读不写的参数配置,如果程序运行过程中需要修改这些环境参数,建议考虑数据库读写方式,而不是properties

    下面是JAVA程序,mian函数一开始就可以对这个properties文件进行读取

    public static void main(String[] args) throws Exception{
            //读取properties配置文件
            Properties prop=new Properties();
            prop.load(QuartzDemo.class.getResourceAsStream("/set.properties"));
            String cron=prop.getProperty("cron","0/10 * 8-7 * * ?");
            String fileName=prop.getProperty("fileName","d:/2014年1月营销活动报表140116.xlsx");
            String sheetName=prop.getProperty("sheetName", "支局视图");
            String dirPath=prop.getProperty("dirPath","C:/apache-tomcat-8.0.28/webapps/test/");
            String urlPath=prop.getProperty("urlPath","http://61.132.43.176:8081/test/");
            String fromIndexStr=prop.getProperty("fromIndex","0,0");
            String toIndexStr=prop.getProperty("toIndex", "17,20");
            
            String database_host=prop.getProperty("database_host","");
            String database_port=prop.getProperty("database_port","");
            String database_user=prop.getProperty("database_user","");
            String database_name=prop.getProperty("database_name","");
            String database_pwd=DESHelper.decrypt(prop.getProperty("database_pwd",""),"newflypig");    
            String sql_str=prop.getProperty("sql_str","");
            String modify_sheet=prop.getProperty("modify_sheet","");
            String modify_fromStr=prop.getProperty("modify_from","0,0");
            
            String[] splitStr=fromIndexStr.split(",");
            int[] fromIndex={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};
            splitStr=toIndexStr.split(",");
            int[] toIndex={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};
            splitStr=modify_fromStr.split(",");
            int[] modify_from={Integer.parseInt(splitStr[0]),Integer.parseInt(splitStr[1])};
    
    }

    Properties类提供了非常便捷的读取properties文件的操作,还包括一些默认值的配置,再次需要注意的是,只能读String,如果遇到其他数据类型,聪明的你一定知道怎样用String类型转换这些类型吧。

    保持着对万物的好奇心。即使是玩游戏,也要停下来想想这是怎么实现的。
  • 相关阅读:
    07-图4 哈利·波特的考试 (25分)
    Windows环境下清除SVN文件
    查看SQL SERVER 2008R2 表大小
    Oauth支持的5类 grant_type 及说明
    SignalR的性能监测
    Loadrunner11安装
    Azure ServiceBus 通信失败问题
    sql server text类型 存储问题
    System.BadImageFormatException
    InputStream只能读取一次的解决办法 C# byte[] 和Stream转换
  • 原文地址:https://www.cnblogs.com/newflydd/p/4926369.html
Copyright © 2011-2022 走看看