conf4j-properties文件的配置工具库-ec文件

描述

properties文件的配置工具库,配置修改动态生效。支持扫描本地目录/git/svn仓库。

使用方法

conf4j-properties文件的配置工具库

配置类

定义配置类

配置文件:demo.properties

demo.attrStr=strValue
demo.attrLong=10000
demo.attrInt=100
demo.attrBool=true

配置类:DemoConf.java

@Config(file = "demo.properties", prefix = "demo")
public class DemoConf implements Conf {
private static transient final Logger LOG = LoggerFactory.getLogger(Conf.class);
public final static DemoConf ME = new DemoConf();
static {
try {
ConfKit.handler(DemoConf.class);
} catch (Exception e) {
LOG.error("handler DemoConf failed.", e);
}
}
String attrStr;
long attrLong;
int attrInt;
boolean attrBool;
//省略getter/setter方法
@Override
public String toString() {
return "DemoConf [attrStr=" + attrStr + ", attrLong=" + attrLong + ", attrInt=" + attrInt + ", attrBool="
+ attrBool + "]";
}
}

调用配置类对象

public class Main {
public static void main(String[] args) throws InterruptedException {
// 通过配置类的me获取实例调用配置属性
System.out.println(DemoConf.ME);
// 通过ConfigManager.config(配置类)获取实例调用配置属性
System.out.println(ConfigManager.config(DemoConf.class));
// 通过ConfigManager.putProperties()追加自定义全局配置属性
Properties properties = new Properties();
String testPropertyKey = "test.add.property.key";
properties.setProperty(testPropertyKey, "test.add.property.value");
ConfigManager.putProperties(properties);
System.out.println(testPropertyKey + "=" + ConfigManager.getValueByKey(testPropertyKey));
}
}

控制台输出

DemoConf [attrStr=strValue, attrLong=10000, attrInt=100, attrBool=true]
DemoConf [attrStr=strValue, attrLong=10000, attrInt=100, attrBool=true]
test.add.property.key=test.add.property.value

配置扫描器

配置扫描器配置

conf4j.properties

##conf4j config扫描器配置
#扫描器类
conf4j.config.scaner.className=com.conf4j.file.scaner.ConfigFileScaner
conf4j.config.scaner.url=
conf4j.config.scaner.user=
conf4j.config.scaner.password=
#扫描的本地配置目录,可使用绝对路径,也支持相对路径
conf4j.config.scaner.localPath=src/test/resources1
#扫描配置的间隔(秒)
conf4j.config.scaner.intervalSec=2
#扫描的配置文件,多个文件以','分割
conf4j.config.scaner.keys=conf4j.properties,demo.properties

启动配置扫描器

public class Main {
public static void main(String[] args) throws InterruptedException {
//启动配置扫描
ConfigManager.startScaner();
int i = 0;
while (i++ < 100) {
Thread.sleep(2000);
System.out.println(DemoConf.ME);
}
}
}

控制台输出

DemoConf [attrStr=strValue, attrLong=10000, attrInt=100, attrBool=true]
DemoConf [attrStr=strValue, attrLong=10000, attrInt=100, attrBool=true]
...

工程介绍

  • conf4j.api支持扫描本地配置目录
  • conf4j.git支持扫描git仓库
  • conf4j.svn支持扫描svn仓库

例子参考conf4j.api/conf4j.git/conf4j.svn中的Main类

-D参数使用

-D参数或conf4j.properties中conf4j.config.scaner.localPath不为空时,配置路径中配置文件覆盖类路径下的配置文件

-Dconf4j.config.scaner.localPath=config/local/path

扩展使用

可以扫描类根据Config注解对配置类自动注入属性,省去static代码块中ConfKit.handle方法的调用。

约束

自己读取配置文件请使用com.conf4j.kit.ClassLoaderKit.getResourceAsStream(String)接口

推荐阅读

    学习写字楼新选择6000元主流配置

    学习写字楼新选择6000元主流配置,,这种配置需要考虑双核心的办公和娱乐平台,充分考虑办公室的办公需求和娱乐需求,以约6000元的预算和cost-e

    酷睿I7 配置

    酷睿I7 配置,配置,玩家国度啦华硕 Rampage II Extreme(3800元)如果米不够,也可以把Extreme改为Gene,不过是小板内存推荐金士顿6G DDR3 2000骇

    鼠标不能拖动文件了

    鼠标不能拖动文件了,鼠标,拖动,本文目录鼠标不能拖动文件了电脑鼠标无法拖动软件图标了,怎么办鼠标不能滚动怎么调整电脑鼠标不能进行任何

    提高3A四核羿龙II游戏配置的性能

    提高3A四核羿龙II游戏配置的性能,,以节能环保为主题的IT产业,目前3A低端平台处理器、主板芯片组、独立开发卡性能突出,特别是在与AMD的处理

    opporeno8参数配置及价格

    opporeno8参数配置及价格,面部,亿元,Oppo的荣誉2020年1月4日,接近屏幕关闭传感器是否支持双卡:支持oppor11splus什么时候上市的Oppo R11S P

    查看配置:酷睿i3530集展示办公平台

    查看配置:酷睿i3530集展示办公平台,,由于时间和精力的关系,我们不可能对所有的配置进行评论,希望我们能理解,我希望我们的评论能在那些需要帮

    3500元超额值学生娱乐结构的优化配置

    3500元超额值学生娱乐结构的优化配置,,作为一个DIY的主流用户领域的学生,每个用户51学生攒机的高峰。因为学生用户没有稳定的收入来源,攒机

    电脑店u修复工具|u盘修复电脑工具

    电脑店u修复工具|u盘修复电脑工具,,u盘修复电脑工具你好,1、电脑管家是没有修复U盘的功能的。所以不好修复U盘的。2、如果要修复U盘的话,可