在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

方便操作Properties的Java类库:props4j

2017-2-7 13:41| 发布者: zhangjf| 查看: 472| 评论: 0

摘要: 方便操作Properties的Java类库。简单和实用的properties工具,没有任何依赖。 Cases Props with auto convert values import base.props4j.impl.MapProps;MapProps props = new MapProps();props.putVal(key1, 1); ...

方便操作Properties的Java类库。简单和实用的properties工具,没有任何依赖。

Cases Props with auto convert values
  1. import base.props4j.impl.MapProps;
  2. MapProps props = new MapProps();
  3. props.putVal("key1", 1);
  4. props.putVal("key2", "val2");
  5. props.putVal("key3", true);
  6. String strVal_1 = props.strVal("key1"); //"1"
  7. long longVal_1 = props.longVal("key1"); //1L
  8. Boolean boolVal_1 = props.boolVal("key1"); //false
复制代码

Default values for empty keys or bad values
  1. int defVal = props.intVal("unknown-key", 42); //42
复制代码
File props with updates check
  1. import base.props4j.impl.FileProps;
  2. import java.io.File;
  3. File file = new File("test.properties");
  4. writeToFile(file, "key1 = 1");
  5. FileProps props = new FileProps(file);
  6. int val = props.intVal("key1"); //1
  7. //update file
  8. writeToFile(file, "key1 = 2");
  9. Thread.sleep(...);
  10. int newVal = props.intVal("key1"); //2
复制代码

MultiProps for many sources
  1. import base.props4j.impl.FileProps;
  2. import base.props4j.impl.MultiProps;
  3. MultiProps props = new MultiProps();
  4. props.addSource(new FileProps(file1));
  5. props.addSource(new FileProps(file2));
复制代码

Enum for keys!
  1. import base.props4j.DefValKey;
  2. public enum Keys implements DefValKey {
  3. planets_in_solar_system(8),
  4. is_frog_green(true),
  5. myName("Evgeny"),
  6. star_wars_episodes_count(999.999d),
  7. ...
  8. }
  9. MapProps props = new MapProps();
  10. int defVal = Keys.planets_in_solar_system.intFrom(props); //8
  11. props.putVal(planets_in_solar_system, 9);
  12. int updatedVal = Keys.planets_in_solar_system.intFrom(props); //9
复制代码

Expressions ${...} in values
  1. props.putVal("url", "127.0.0.1");
  2. props.putVal("link1", "${url}/some1");
  3. props.putVal("link2", "${url}/some2");
  4. String link1 = props.strVal("link1"); // 127.0.0.1/some1
  5. String link2 = props.strVal("link2"); // 127.0.0.1/some2
复制代码

Code examples Example 1: Simple MapProps and basic get-put methods Example 2: FileProps for file properties Example 3: FileProps and file updated check Example 4: MultiProps for many props sources Example 5: Java Enums are best keys for props! Example 6: Expressions ${...} in values Production ready

Props4j has been successfully used on Cheapchat.me and in open source live-chat-engine project.

Installation to your project

Java 8 is required (sources can easily be ported to Java 6 or 7 -- but I'm on Java 8 now :))

by Maven

via stackoverflow

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.github.edolganov</groupId>
  4. <artifactId>props4j</artifactId>
  5. <version>1.0</version>
  6. </dependency>
  7. </dependencies>
  8. <repositories>
  9. <repository>
  10. <id>jitpack.io</id>
  11. <url>https://jitpack.io</url>
  12. </repository>
  13. </repositories>
复制代码

项目主页:http://www.open-open.com/lib/view/home/1451046744651

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 07:25

Copyright 2015-2025 djqfx

返回顶部