java property 配置檔案管理工具框架,避免寫入 property 亂序
阿新 • • 發佈:2019-11-21
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
為需要設定的屬性資訊。
引導類方法概覽
序號 | 方法 | 說明 |
---|---|---|
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
別稱類似。
後續特性
- 提供更多內建的型別轉換實現