java property配置文件管理工具框架过程详解
这篇文章主要介绍了javaproperty配置文件管理工具框架过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
property
property是java实现的property框架。
特点
- 优雅地进行属性文件的读取和更新
- 写入属性文件后属性不乱序
- 灵活定义编码信息
- 使用OO的方式操作property文件
- 支持多级对象引用
快速开始
环境依赖
Maven3.x
Jdk1.7+
Maven引入依赖
com.github.houbb property 0.0.4
入门案例
读取属性
PropertyBs.getInstance("read.properties").get("hello");
read.properties为文件路径,hello为存在的属性值名称。
读取属性指定默认值
finalStringvalue=PropertyBs.getInstance("read.properties")
.getOrDefault("hello2","default");
read.properties为文件路径,hello2为不存在的属性值名称,default为属性不存在时返回的默认值。
设置属性
PropertyBs.getInstance("writeAndFlush.properties").setAndFlush("hello","world-set");
writeAndFlush.properties为文件路径,hello为需要设置的属性信息。
引导类方法概览
序号 | 方法 | 说明 |
---|---|---|
1 | getInstance(propertyPath) | 获取指定属性文件路径的引导类实例 |
2 | charset(charset) | 指定文件编码,默认为 UTF-8 |
3 | get(key) | 获取key对应的属性值 |
4 | getOrDefault(key,defaultValue) | 获取key对应的属性值,不存在则返回defaultValue |
5 | set(key,value) | 设置值(内存) |
6 | remove(key) | 移除值(内存) |
7 | flush() | 刷新内存变更到当前文件磁盘 |
9 | flush(path) | 刷新内存变更到指定文件磁盘 |
10 | set(map) | 设置map信息到内存 |
11 | set(bean) | 设置bean对象信息到内存 |
12 | asMap() | 返回内存中属性信息,作为Map返回 |
13 | asBean(bean) | 返回内存中属性信息到bean对象中 |
对象
简介
我们希望操作property可以想操作对象一样符合OO的思想。
设置值
Useruser=newUser(); user.setName("hello"); user.setHobby("hobby"); finallongtime=1574147668411L; user.setBirthday(newDate(time)); PropertyBspropertyBs=PropertyBs.getInstance("setBean.properties") .set(user); Assert.assertEquals("hobby",propertyBs.get("myHobby")); Assert.assertEquals("1574147668411",propertyBs.get("birthday"));
读取值
PropertyBspropertyBs=PropertyBs.getInstance("setBean.properties" .set("myHobby","play") .set("birthday","1574147668411"); Useruser=newUser(); propertyBs.asBean(user); Assert.assertEquals("play",user.getHobby()); Assert.assertEquals(1574147668411L,user.getBirthday().getTime());
对象定义
User.java
publicclassUser{ privateStringname; @PropertyField("myHobby") privateStringhobby; @PropertyField(converter=DateValueConverter.class) privateDatebirthday; }
@PropertyField注解
序号 | 属性 | 默认值 | 说明 |
---|---|---|---|
1 | value | 当前字段名称 | 对应的property属性名称 |
2 | converter | 默认转换实现 DefaultValueConverter | 对当前字段进行属性的转换处理 |
自定义转换类
DateValueConverter.java
这个就是我们针对Date类型,自己实现的处理类型。
实现如下:
publicclassDateValueConverterimplementsIValueConverter{ @Override publicObjectfieldValue(Stringvalue,IFieldValueContextcontext){ returnnewDate(Long.parseLong(value)); } @Override publicStringpropertyValue(Objectvalue,IPropertyValueContextcontext){ Datedate=(Date)value; returndate.getTime()+""; } }
集合
说明
有时候一个属性可能是集合或者数组,这里暂时给出比较简单的实现。
将字段值直接根据逗号分隔,作为属性值。
测试案例
UserArrayCollectionuserArrayCollection=buildUser(); PropertyBspropertyBs=PropertyBs.getInstance("setBeanArrayCollection.properties") .set(userArrayCollection); Assert.assertEquals("array,collection",propertyBs.get("alias")); Assert.assertEquals("array,collection",propertyBs.get("hobbies"));
对象定义
UserArrayCollection.java
publicclassUserArrayCollection{ privateListalias; privateString[]hobbies; }
暂时只支持String类型,不想做的过于复杂。
后期将考虑添加各种类型的支持。
多级对象
说明
有时候我们在一个对象中会引用其他对象,比如对象a中包含对象b。
这里采用a.b.c这种方式作为属性的key,更加符合使用的习惯。
测试案例
设置
Bookbook=newBook(); book.name("《海底两万里》").price("12.34"); UserEntryuser=newUserEntry(); user.name("海伦").book(book).age("10"); PropertyBspropertyBs=PropertyBs.getInstance("setBeanEntry.properties") .set(user); Assert.assertEquals("海伦",propertyBs.get("name")); Assert.assertEquals("10",propertyBs.get("age")); Assert.assertEquals("《海底两万里》",propertyBs.get("book.name")); Assert.assertEquals("12.34",propertyBs.get("book.price"));
读取
Mapmap=newHashMap<>(); map.put("name","海伦"); map.put("age","10"); map.put("book.name","《海底两万里》"); map.put("book.price","12.34"); UserEntryuserEntry=newUserEntry(); PropertyBs.getInstance("setBeanEntry.properties") .set(map) .asBean(userEntry); Assert.assertEquals("UserEntry{name='海伦',age=10,book=Book{name='《海底两万里》',price=12.34}}", userEntry.toString());
对象定义
UserEntry.java
publicclassUserEntry{ privateStringname; privateStringage; @PropertyEntry privateBookbook; }
Book.java
publicclassBook{ privateStringname; privateStringprice; }
@PropertyEntry说明
@PropertyEntry注解用来标识一个字段是否采用多级对象的方式表示。
这个注解只有一个属性,就是value(),可以用来给当前字段指定一个别称,和@PropertyField别称类似。
后续特性
提供更多内置的类型转换实现
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。