java非web应用修改 properties/xml配置文件后,无需重启应用即可生效---自动加载

时间:2022-03-26 11:58:49

实现时主要使用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>