java property配置文件管理工具框架過(guò)程詳解
這篇文章主要介紹了java property配置文件管理工具框架過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
property
property 是 java 實(shí)現(xiàn)的 property 框架。
特點(diǎn)
- 優(yōu)雅地進(jìn)行屬性文件的讀取和更新
- 寫(xiě)入屬性文件后屬性不亂序
- 靈活定義編碼信息
- 使用 OO 的方式操作 property 文件
- 支持多級(jí)對(duì)象引用
快速開(kāi)始
環(huán)境依賴(lài)
Maven 3.x
Jdk 1.7+
Maven 引入依賴(lài)
<dependency> <groupId>com.github.houbb</groupId> <artifactId>property</artifactId> <version>0.0.4</version> </dependency>
入門(mén)案例
讀取屬性
PropertyBs.getInstance("read.properties").get("hello");
read.properties 為文件路徑,hello 為存在的屬性值名稱(chēng)。
讀取屬性指定默認(rèn)值
final String value = PropertyBs.getInstance("read.properties")
.getOrDefault("hello2", "default");
read.properties 為文件路徑,hello2 為不存在的屬性值名稱(chēng),default 為屬性不存在時(shí)返回的默認(rèn)值。
設(shè)置屬性
PropertyBs.getInstance("writeAndFlush.properties").setAndFlush("hello", "world-set");
writeAndFlush.properties 為文件路徑,hello 為需要設(shè)置的屬性信息。
引導(dǎo)類(lèi)方法概覽
| 序號(hào) | 方法 | 說(shuō)明 |
|---|---|---|
| 1 | getInstance(propertyPath) | 獲取指定屬性文件路徑的引導(dǎo)類(lèi)實(shí)例 |
| 2 | charset(charset) | 指定文件編碼,默認(rèn)為 UTF-8 |
| 3 | get(key) | 獲取 key 對(duì)應(yīng)的屬性值 |
| 4 | getOrDefault(key, defaultValue) | 獲取 key 對(duì)應(yīng)的屬性值,不存在則返回 defaultValue |
| 5 | set(key, value) | 設(shè)置值(內(nèi)存) |
| 6 | remove(key) | 移除值(內(nèi)存) |
| 7 | flush() | 刷新內(nèi)存變更到當(dāng)前文件磁盤(pán) |
| 9 | flush(path) | 刷新內(nèi)存變更到指定文件磁盤(pán) |
| 10 | set(map) | 設(shè)置 map 信息到內(nèi)存 |
| 11 | set(bean) | 設(shè)置 bean 對(duì)象信息到內(nèi)存 |
| 12 | asMap() | 返回內(nèi)存中屬性信息,作為 Map 返回 |
| 13 | asBean(bean) | 返回內(nèi)存中屬性信息到 bean 對(duì)象中 |
對(duì)象
簡(jiǎn)介
我們希望操作 property 可以想操作對(duì)象一樣符合 OO 的思想。
設(shè)置值
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());
對(duì)象定義
User.java
public class User {
private String name;
@PropertyField("myHobby")
private String hobby;
@PropertyField(converter = DateValueConverter.class)
private Date birthday;
}
@PropertyField 注解
| 序號(hào) | 屬性 | 默認(rèn)值 | 說(shuō)明 |
|---|---|---|---|
| 1 | value | 當(dāng)前字段名稱(chēng) | 對(duì)應(yīng)的 property 屬性名稱(chēng) |
| 2 | converter | 默認(rèn)轉(zhuǎn)換實(shí)現(xiàn) DefaultValueConverter | 對(duì)當(dāng)前字段進(jìn)行屬性的轉(zhuǎn)換處理 |
自定義轉(zhuǎn)換類(lèi)
DateValueConverter.java
這個(gè)就是我們針對(duì) Date 類(lèi)型,自己實(shí)現(xiàn)的處理類(lèi)型。
實(shí)現(xiàn)如下:
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()+"";
}
}
集合
說(shuō)明
有時(shí)候一個(gè)屬性可能是集合或者數(shù)組,這里暫時(shí)給出比較簡(jiǎn)單的實(shí)現(xiàn)。
將字段值直接根據(jù)逗號(hào)分隔,作為屬性值。
測(cè)試案例
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"));
對(duì)象定義
UserArrayCollection.java
public class UserArrayCollection {
private List<String> alias;
private String[] hobbies;
}
暫時(shí)只支持 String 類(lèi)型,不想做的過(guò)于復(fù)雜。
后期將考慮添加各種類(lèi)型的支持。
多級(jí)對(duì)象
說(shuō)明
有時(shí)候我們?cè)谝粋€(gè)對(duì)象中會(huì)引用其他對(duì)象,比如 對(duì)象 a 中包含對(duì)象 b。
這里采用 a.b.c 這種方式作為屬性的 key, 更加符合使用的習(xí)慣。
測(cè)試案例
設(shè)置
Book book = new Book();
book.name("《海底兩萬(wàn)里》").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("《海底兩萬(wàn)里》", 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", "《海底兩萬(wàn)里》");
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='《海底兩萬(wàn)里》', price=12.34}}",
userEntry.toString());
對(duì)象定義
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 說(shuō)明
@PropertyEntry 注解用來(lái)標(biāo)識(shí)一個(gè)字段是否采用多級(jí)對(duì)象的方式表示。
這個(gè)注解只有一個(gè)屬性,就是 value(),可以用來(lái)給當(dāng)前字段指定一個(gè)別稱(chēng),和 @PropertyField 別稱(chēng)類(lèi)似。
后續(xù)特性
提供更多內(nèi)置的類(lèi)型轉(zhuǎn)換實(shí)現(xiàn)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)PDF轉(zhuǎn)圖片的三種方法
有些時(shí)候我們需要在項(xiàng)目中展示PDF,所以我們可以將PDF轉(zhuǎn)為圖片,然后已圖片的方式展示,效果很好,Java使用各種技術(shù)將pdf轉(zhuǎn)換成圖片格式,并且內(nèi)容不失幀,本文給大家介紹了三種方法實(shí)現(xiàn)PDF轉(zhuǎn)圖片的案例,需要的朋友可以參考下2023-10-10
SpringBoot?基于?MongoTemplate?的工具類(lèi)過(guò)程詳解
MongoDB是一個(gè)高性能,開(kāi)源,無(wú)模式的文檔型數(shù)據(jù)庫(kù),是當(dāng)前NoSql數(shù)據(jù)庫(kù)中比較熱門(mén)的一種,這篇文章主要介紹了SpringBoot基于MongoTemplate的工具類(lèi),需要的朋友可以參考下2023-09-09
RocketMQ之Consumer整體介紹啟動(dòng)源碼分析
這篇文章主要為大家介紹了RocketMQ源碼分析之Consumer整體介紹啟動(dòng)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
BeanUtils.copyProperties復(fù)制不生效的解決
這篇文章主要介紹了BeanUtils.copyProperties復(fù)制不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
如何實(shí)現(xiàn)springboot中controller之間的相互調(diào)用
這篇文章主要介紹了實(shí)現(xiàn)springboot中controller之間的相互調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java 定時(shí)同步數(shù)據(jù)的任務(wù)優(yōu)化
這篇文章主要介紹了java 定時(shí)同步數(shù)據(jù)的任務(wù)優(yōu)化,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12

