property
property 是 java 实现的 property 框架。

扎兰屯网站建设公司创新互联,扎兰屯网站设计制作,有大型网站制作公司丰富经验。已为扎兰屯数千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的扎兰屯做网站的公司定做!
特点
- 优雅地进行属性文件的读取和更新 
- 写入属性文件后属性不乱序 
- 灵活定义编码信息 
- 使用 OO 的方式操作 property 文件 
- 支持多级对象引用
变更日志
ChangeLog
快速开始
环境依赖
Maven 3.x
Jdk 1.7+
Maven 引入依赖
    com.github.houbb 
    property 
    0.0.4 
 入门案例
读取属性
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 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 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 别称类似。
后续特性
- 提供更多内置的类型转换实现
分享文章:javaproperty配置文件管理工具框架,避免写入property乱序
本文链接:http://www.cqwzjz.cn/article/gspiji.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 