java非web应用修改 properties/xml配置文件后,无需重启应用即可生效---自动加载
实现时主要使用Commons-Configuration.jar包,还需要commons-lang,disgestor,beanutils,collections等,
package propFile;
import java.net.URL;
import java.util.List;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
/**
* 修改 properties 文件时,每次都要重启应用程序, Commons-Configuration 调用 getXxxx 方法获取到的内容都是
* properties 文件中最新的,无需重启应用
*
* @author zhaigx
* @DATA 2011-5-27
*/
public class PropertiesUtil {
static String propertiesFile = "config/config.properties";
static PropertiesConfiguration propConfig;
static Configuration multiConfig;
static boolean isReload = true;
private PropertiesUtil() {
}
private static void reloadPropFile() {
if (isReload) {
try {
propConfig = new PropertiesConfiguration(propertiesFile);
propConfig
.setReloadingStrategy(new FileChangedReloadingStrategy());
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
isReload = false;
}
public static Object getValueFromPropFile(String key) {
reloadPropFile();
return propConfig.getProperty(key);
}
public static String[] getArrFromPropFile(String key){
reloadPropFile();
return propConfig.getStringArray(key);
}
private static void reloadMultiConfigFile() {
ConfigurationFactory factory = new ConfigurationFactory();
String file = "config/xml-prop.xml";
System.out.println("multiConfigFile==> " + file);
factory.setConfigurationFileName(file);
try {
multiConfig = factory.getConfiguration();
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Object getValueFromMultiFile(String key) {
reloadMultiConfigFile();
return multiConfig.getProperty(key);
}
}
测试类:
package propFile;
import java.util.List;
import propFile.PropertiesUtil;
public class PropertiesUtilTest {
/**
*
* @author zhaigx
* @param args
* @DATE 2011-5-27
*/
public static void main(String[] args) {
// for (int i = 0; i < 1000; i++) {
// String valueString = (String) PropertiesUtil
// .getValueFromPropFile("name");
// System.out.println(valueString);
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block'
// e.printStackTrace();
// }
// }
String[] country=(String[])PropertiesUtil.getArrFromPropFile("country");
for (int i = 0; i < country.length; i++) {
System.out.println(country[i]);
}
List list = (List) PropertiesUtil
.getValueFromMultiFile("start-criteria.criteria");
for (int i = 0; i < list.size(); i++) {
String value = list.get(i).toString();
System.out.println(value);
}
for (int i = 0; i < 1000; i++) {
int intValue = Integer.parseInt(PropertiesUtil
.getValueFromMultiFile("horsepower").toString());
System.out.println(intValue);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
config目录下的配置文件:
config.properties:
a=1
b=3
name=to
country = USA,China,English
xml-prop.xml:
<?xml version="1.0" encoding="gbk" ?>
<configuration>
<properties fileName="global.properties"></properties>
<xml fileName="global.xml"></xml>
</configuration>
global.properties:
threads.maximum=50
threads.minimum=20
timeout=15.52
interactive=true
color=red
speed=50
name=Default User
region=Earth
global.xml:
<?xml version="1.0" encoding="gbk" ?>
<engine-config>
<start-criteria>
<criteria>
Temperature Above -10 Celsius
</criteria>
<criteria>
Fuel tank is not empty
</criteria>
</start-criteria>
<volume>4 Liters</volume>
<horsepower>77</horsepower>
</engine-config>
java非web应用修改 properties/xml配置文件后,无需重启应用即可生效---自动加载的更多相关文章
- (转)ALSA配置文件(alsa.conf, asoundrc, asound.conf)及其自动加载 And HDMI Adiuo
原文出处:http://blog.sina.com.cn/s/blog_a04184c101010kry.html 警告:错误的EDID会造成HDMI发声异常 #title:box:HDMI Audi ...
- web.xml的contextConfigLocation作用及自动加载applicationContext.xml
web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...
- Logstash配置文件修改自动加载和指定目录进行启动
检查配置并启动Logstash,修改后自动加载 指定配置文件目录并启动Logstash
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- spring的applicationContext.xml如何自动加载
一个web工程自动加载的配置文件只有web.xml,想要加载其他.xml必须在web.xml里面进行配置. 用spring的时候需要一个bean容器来管理所有的bean,所有bean默认是写在appl ...
- spring boot修改代码后无需重启设置,在开发时实现热部署
Spring Boot在开发时实现热部署(开发时修改文件保存后自动重启应用)(spring-boot-devtools) 热部署是什么 大家都知道在项目开发过程中,常常会改动页面数据或者修改数据结构, ...
- iOS开发 XML解析和下拉刷新,上拉加载更多
iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
随机推荐
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- 【Spark】Spark-foreachRDD需要注意的问题
Spark-foreachRDD需要注意的问题 dstream.foreachRDD_百度搜索 通过Spark Streaming的foreachRDD把处理后的数据写入外部存储系统中 - 吾心光明 ...
- 【转】vim中多标签和多窗口的使用
原文:https://my.oschina.net/kutengshe/blog/464602 ---------------------------------------------------- ...
- async和await的返回值——NodeJS, get return value from async await
在ES6和ES5中promise的执行也有不同点(上述提到,ES6中promise属microtask:在ES5中,暂未接触到有api直接操作microtask的,所以.then的异步是用setTim ...
- Using Timers in MFC Applications
Timer Events in MFC Applications Event timers are always handy to have around and useful in nearly e ...
- XE6入门(二)项目中的文件
XE6中项目文件为DPR,查看方法请参考一下以前写过的博文: "Delphi项目构成之项目文件DPR" 项目文件DPR 通过主菜单[Project | View Source],就 ...
- [Canvas]游戏增加怪物爆炸效果,改善箭头形状
请点此下载代码并用浏览器打开试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-eq ...
- LCX端口内网映射转发
这几天在渗透一家奶茶店的时候, 使用溢出攻击获得了奶茶店shell, 截屏以后发现该电脑有安装sqlserver, 但是数据库无法远程连接, 所以我决定使用端口映射工具突破端口连接限制: 我的机器地址 ...
- Swift语言精要 - 序列化和反序列化
在swift中你可以把一个对象转换成为数据,你所要做的就是 首先,你需要让对象实现NSObject和NSCoding协议. 其次,实现以下两个方法: encodeWithCoder init(code ...
- GetParam(name)
function GetParam(name) { var match = new RegExp(name + "=*([^&]+)*", "i").e ...