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

发布时间:2020-07-03 07:52:15 作者:叶止水ryo
来源:网络 阅读:137160

property

property 是 java 实现的 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 为需要设置的属性信息。

引导类方法概览

序号 方法 说明
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());

对象定义

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 对当前字段进行属性的转换处理

自定义转换类

这个就是我们针对 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"));

对象定义

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());

对象定义

public class UserEntry {

    private String name;

    private String age;

    @PropertyEntry
    private Book book;

}
public class Book {

    private String name;

    private String price;

}

@PropertyEntry 说明

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

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

后续特性

推荐阅读:
  1. java单例模式的实现方式以及差异
  2. java property配置文件管理工具框架过程详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

properties property ava

上一篇:Ubuntu14.04搭建LAMP

下一篇:MySQL 管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》