property

property 是 java 实现的 property 框架。

特点

优雅地进行属性文件的读取和更新

写入属性文件后属性不乱序

灵活定义编码信息

使用 OO 的方式操作 property 文件

支持多级对象引用变更日志

ChangeLog

快速开始环境依赖

Maven 3.x

Jdk 1.7+

Maven 引入依赖

<dependency> <groupId>com.github.houbb</groupId> <artifactId>property</artifactId> <version>0.0.4</version></dependency>入门案例读取属性

PropertyBs.getInstance("read.properties").get("hello");

read.properties 为文件路径,hello 为存在的属性值名称。

读取属性指定默认值

final String value = PropertyBs.getInstance("read.properties") .getOrDefault("hello2", "default");

read.properties 为文件路径,hello2 为不存在的属性值名称,default 为属性不存在时返回的默认值。

设置属性

PropertyBs.getInstance("writeAndFlush.properties").setAndFlush("hello", "world-set");

writeAndFlush.properties 为文件路径,hello 为需要设置的属性信息。

引导类方法概览序号方法说明1getInstance(propertyPath)获取指定属性文件路径的引导类实例2charset(charset)指定文件编码,默认为 UTF-83get(key)获取 key 对应的属性值4getOrDefault(key, defaultValue)获取 key 对应的属性值,不存在则返回 defaultValue5set(key, value)设置值(内存)6remove(key)移除值(内存)7flush()刷新内存变更到当前文件磁盘9flush(path)刷新内存变更到指定文件磁盘10set(map)设置 map 信息到内存11set(bean)设置 bean 对象信息到内存12asMap()返回内存中属性信息,作为 Map 返回13asBean(bean)返回内存中属性信息到 bean 对象中对象简介

我们希望操作 property 可以想操作对象一样符合 OO 的思想。

设置值

User user = new User();user.setName("hello");user.setHobby("hobby");final long time = 1574147668411L;user.setBirthday(new Date(time));PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties") .set(user);Assert.assertEquals("hobby", propertyBs.get("myHobby"));Assert.assertEquals("1574147668411", propertyBs.get("birthday"));读取值

PropertyBs propertyBs = PropertyBs.getInstance("setBean.properties" .set("myHobby", "play") .set("birthday", "1574147668411");User user = new User();propertyBs.asBean(user);Assert.assertEquals("play", user.getHobby());Assert.assertEquals(1574147668411L, user.getBirthday().getTime());对象定义User.java

public class User { private String name; @PropertyField("myHobby") private String hobby; @PropertyField(converter = DateValueConverter.class) private Date birthday;}@PropertyField 注解序号属性默认值说明1value当前字段名称对应的 property 属性名称2converter默认转换实现 DefaultValueConverter对当前字段进行属性的转换处理自定义转换类DateValueConverter.java

这个就是我们针对 Date 类型,自己实现的处理类型。

实现如下:

public class DateValueConverter implements IValueConverter { @Override public Object fieldValue(String value, IFieldValueContext context) { return new Date(Long.parseLong(value)); } @Override public String propertyValue(Object value, IPropertyValueContext context) { Date date = (Date)value; return date.getTime()+""; }}集合说明

有时候一个属性可能是集合或者数组,这里暂时给出比较简单的实现。

将字段值直接根据逗号分隔,作为属性值。

测试案例

UserArrayCollection userArrayCollection = buildUser();PropertyBs propertyBs = PropertyBs.getInstance("setBeanArrayCollection.properties") .set(userArrayCollection);Assert.assertEquals("array,collection", propertyBs.get("alias"));Assert.assertEquals("array,collection", propertyBs.get("hobbies"));对象定义UserArrayCollection.java

public class UserArrayCollection { private List<String> alias; private String[] hobbies;}

暂时只支持 String 类型,不想做的过于复杂。

后期将考虑添加各种类型的支持。

多级对象说明

有时候我们在一个对象中会引用其他对象,比如 对象 a 中包含对象 b。

这里采用 a.b.c 这种方式作为属性的 key, 更加符合使用的习惯。

测试案例设置

Book book = new Book();book.name("《海底两万里》").price("12.34");UserEntry user = new UserEntry();user.name("海伦").book(book).age("10");PropertyBs propertyBs = 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"));读取

Map<String, String> map = new HashMap<>();map.put("name", "海伦");map.put("age", "10");map.put("book.name", "《海底两万里》");map.put("book.price", "12.34");UserEntry userEntry = new UserEntry();PropertyBs.getInstance("setBeanEntry.properties") .set(map) .asBean(userEntry);Assert.assertEquals("UserEntry{name='海伦', age=10, book=Book{name='《海底两万里》', price=12.34}}", userEntry.toString());对象定义UserEntry.java

public class UserEntry { private String name; private String age; @PropertyEntry private Book book;}Book.java

public class Book { private String name; private String price;}@PropertyEntry 说明

@PropertyEntry 注解用来标识一个字段是否采用多级对象的方式表示。

这个注解只有一个属性,就是 value(),可以用来给当前字段指定一个别称,和 @PropertyField 别称类似。

后续特性提供更多内置的类型转换实现