java property 配置文件管理工具框架,避免写入 property 乱序

property

property 是 java 实现的 property 框架。java

特色

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

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

  • 灵活定义编码信息数组

  • 使用 OO 的方式操做 property 文件框架

  • 支持多级对象引用

变动日志

ChangeLogide

快速开始

环境依赖

Maven 3.x测试

Jdk 1.7+ui

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 为须要设置的属性信息。

引导类方法概览

序号 方法 说明
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 的思想。

设置值

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 注解

序号 属性 默认值 说明
1 value 当前字段名称 对应的 property 属性名称
2 converter 默认转换实现 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 别称相似。

后续特性

  • 提供更多内置的类型转换实现
相关文章
相关标签/搜索