细数Java项目中用过的配置文件(properties篇)
灵魂拷问:在不重启服务的前提下,如何让配置修改生效的呢?有什么奇技淫巧吗?
灵魂拷问:在 Java 项目中,总能看到以 .properties 为后缀的文件踪影,这类配置文件是怎么加载的呢?
项目研发过程中,总会遇到一些经常改变的参数,比如要连接的数据库的连接地址、名称、用户名、密码;再比如访问三方服务的 URL 等等。考虑到程序的通用性,这些参数往往不能直接写死在程序里,通常借助配置文件来优雅处理。
在 Java 项目中,properties 文件当属使用较简单一类,不过虽然简单,还是要好好说说项目中都是怎么使用的,尝试通过源码解读,让你真正懂它,并带你深刻体会 Java 中重载的意义。
1. 虽说简单,格式还是要看一看。
相比上次谈及的 ini 配置文件,properties 文件格式没有 Section (节)的概念,反而是简单了不少。
上图是一个 jdbc 连接所需要的配置,其中以 # 开始的每一行是注释信息,而以等号分割的每行配置,就是常说的键-值对,等号左边的为 key(代码中的变量),等号右边的为 value(是依据实际场景而配置的值)。
2. 虽说简单,Java 源码还是去要看看。
在 Java 中提供了 java.util.Properties 类,主要用于对配置文件的读写操作。
一图掌握血缘关系,很显然 Properties 继承自 Hashtable,归根结底是个 Map,而 Properties 最特殊的地方,就是它的键和值都是字符串类型。
从全局了解梗概,然后走进 JDK 源码,按照思路,步步去深入。
首先,要看看写好的配置文件是怎么加载的?
源码很清晰,提供字符流 Reader、字节流 InputStream两种方式加载配置文件(方法重载的目的:让使用者更方便),再深入去看最终会调用 Hashtable 的 put(key, value) 来设置键值对,最终完成配置文件中的加载。
然后,要看看怎么根据 key 获得对应的 value(放进去了,还要考虑拿出来)?
源码很清晰,通过参数 key 获得对应的 value,考虑到使用者的方便,对 getProperty 方法进行了重载,其中标注 2 的方法,当根据 key 获得值是 null 时,会返回一个调用方法时传入的默认值(很多场景下,确实很有用)。
知道了怎么加载配置文件,知道了怎么获取 key 对应的值,按照常理说,项目中已经够用了,但是有些时候项目启动后,还真需要再额外设置一下参数的值,不过没关系,因为 Java 已经想到了这一点,对外提供了 setProperty 的方法,让额外设置参数成为可能。
若能在项目研发中,熟练使用上面提到的这些 API,已经足矣。既然打开了源码,索性把杂七杂八的都提提,说不定某些 API 也能解决你碰到的其它场景的问题呢。
Properties 类不仅提供了 load 进行加载配置,而且还提供了把键值对写到文件中的能力。如上面源码所示,考虑到使用者的方便,对 store 方法进行重载,提供了面向字节流 OutputStream、字符流 Writer 两种方式的 store。
如上面源码所示,Properties 除了提供对常规配置文件读写能力支撑,对 xml 配置文件加载、写入也提供了支撑。
如上图源码所示,Properties 类提供了重载的 list 方法,为了方便调试,可以把键值对列表给整齐的打印出来。
3. 虽说简单,不能赋予实践一切都是扯淡。
场景一:在 APM 性能监控时,获取 Java 应用画像信息常用 API。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
程序跑起来,部分输出截图示意如下。
场景二:业务开发中,让配置替代硬编码,并考虑配置更新时,程序能够读到最新的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
借助开篇提到的 jdbc 配置文件,进行验证。尝试获取数据库类型,默认配置为 db2,中途修改参数的值为 mysql,看看效果如何?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
程序跑起来,效果还是让人很满意。
4. 虽说简单,洋洋洒洒分享一大篇。
有关配置文件的分享网上有很多,而我们的分享却显得不太一样。
我们的初衷是:结合实际项目及源码,说说这些年用过的那些有关配置的奇技淫巧,帮你提高研发能力(那怕是提高一丢丢,就算成功)。
它山之石可以攻玉,相信会对你有所帮助。
为了能够帮你提高研发能力(那怕是提高一丢丢呢),后续将继续结合实际项目,看看用到的其它形式的配置文件,敬请期待。
原文:https://www.cnblogs.com/socoool/p/12683501.html