PropertiesConfiguration的用法

PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法。

使用org.apache.commons.configuration2中提供的工具来读取属性文件 
1.创建Java工程 
2.引入所需jar包 
工具所需jar包 
3.在src下创建属性文件properties.properties,内容如下:

username=eclipse
password=123456

 

4.创建工具类PropsUtils

public class PropertyUtils {
    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, IOException {
    String relativelyPath = System.getProperty("user.dir");//获取当前项目的根目录
    PropertiesConfiguration config = new PropertiesConfiguration();
    config.read(new FileReader(relativelyPath + "/src/properties.properties"));
    String username = config.getString("username");
    System.out.println(username);
    String password = config.getString("password");
    System.out.println(password);
    }
}

它来自commons-configuration-1.6.jar。

直接上代码:

[java] view plain copy
 
 print?
  1. public class Config {  
  2.     private static PropertiesConfiguration config = null;  
  3.       
  4.     static {  
  5.         try {  
  6.             config = new PropertiesConfiguration(  
  7.                     "config.properties");  
  8.         } catch (ConfigurationException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.     }  
  12.       
  13.     public static String getString(String key) {  
  14.         return config.getString(key);  
  15.     }  
  16.   
  17.     public static String getString(String key, String def) {  
  18.         return config.getString(key, def);  
  19.     }  
  20.   
  21.     public static int getInt(String key) {  
  22.         return config.getInt(key);  
  23.     }  
  24.   
  25.     public static int getInt(String key, int def) {  
  26.         return config.getInt(key, def);  
  27.     }  
  28.   
  29.     public static long getLong(String key) {  
  30.         return config.getLong(key);  
  31.     }  
  32.   
  33.     public static long getLong(String key, long def) {  
  34.         return config.getLong(key, def);  
  35.     }  
  36.   
  37.     public static float getFloat(String key) {  
  38.         return config.getFloat(key);  
  39.     }  
  40.   
  41.     public static float getFloat(String key, float def) {  
  42.         return config.getFloat(key, def);  
  43.     }  
  44.   
  45.     public static double getDouble(String key) {  
  46.         return config.getDouble(key);  
  47.     }  
  48.   
  49.     public static double getDouble(String key, double def) {  
  50.         return config.getDouble(key, def);  
  51.     }  
  52.   
  53.     public static boolean getBoolean(String key) {  
  54.         return config.getBoolean(key);  
  55.     }  
  56.   
  57.     public static boolean getBoolean(String key, boolean def) {  
  58.         return config.getBoolean(key, def);  
  59.     }  
  60.   
  61.     public static String[] getStringArray(String key) {  
  62.         return config.getStringArray(key);  
  63.     }  
  64.   
  65.     @SuppressWarnings("unchecked")  
  66.     public static List getList(String key) {  
  67.         return config.getList(key);  
  68.     }  
  69.   
  70.     @SuppressWarnings("unchecked")  
  71.     public static List getList(String key, List def) {  
  72.         return config.getList(key, def);  
  73.     }  
  74.   
  75.     public static void setProperty(String key, Object value) {  
  76.         config.setProperty(key, value);  
  77.     }  
  78.   
  79. }  

Commons Configuration是一个Java应用程序的配置管理类库。可以从properties或者xml文件中加载软件的配置信息,用来构建支撑软件运 行的基础环境。在一些配置文件较多较的复杂的情况下,使用该配置工具比较可以简化配置文件的解析和管理。也提高了开发效率和软件的可维护性。

官方主页:http://commons.apache.org/configuration/

它目前支持的配置文件格式有:

Properties files 
XML documents 
Windows INI files 
Property list files (plist) 
JNDI 
JDBC Datasource 
System properties 
Applet parameters 
Servlet parameters

我使用的是目前最新版本1.9,以调用 Properties格式的文件为例,使用方法如下:

基本用法:

1.加载jar包,我使用maven自动加载,pom.xml配置如下:

 

[html] view plain copy
 
 print?
  1. <dependency>  
  2.     <groupId>commons-configuration</groupId>  
  3.     <artifactId>commons-configuration</artifactId>  
  4.     <version>1.9</version>  
  5. </dependency>  
  6. <!-- commons-configuration 自动加载的是2.1的版本,编译时会报错,所以再加上这个 -->  
  7. <dependency>  
  8.     <groupId>commons-lang</groupId>  
  9.     <artifactId>commons-lang</artifactId>  
  10.     <version>2.6</version>  
  11. </dependency>  

common-lang这个包要用新版的,如果不写这个依赖,commons-configuration会下载一个2.1旧版,导致编译出错

2.java代码:

 

[java] view plain copy
 
 print?
  1. PropertiesConfiguration config = new PropertiesConfiguration(“/database.properties”);  
  2. String userName = config.getString("name");  

 

除了getString()方法外,还有getBoolean,getDouble,getInteger等不同返回类型的方法可以调用。

 

进阶用法:

一个项目有会有多个配置文件,这时有个统一的配置文件管理类就很有必要了,我写了一个简单的,大家可以参考下,有不妥的用法也请指出来

1.java类

 

[java] view plain copy
 
 print?
  1. package com.xxx.xxx.util;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.commons.configuration.ConfigurationException;  
  7. import org.apache.commons.configuration.PropertiesConfiguration;  
  8. /** 
  9.  * <p> 
  10.  * 读取配置文件类 
  11.  * </p> 
  12.  * <p> 
  13.  * 根据配置文件名和属性key返回属性内容,configUtil.get(configFile, property); 
  14.  * </p> 
  15.  * @author shengzhi.rensz 
  16.  * 
  17.  */  
  18. public class configUtil {  
  19.   
  20.     private static configUtil initor = new configUtil();     
  21.       
  22.     private static Map<String, Object> configMap = new HashMap<String, Object>();  
  23.       
  24.     private configUtil() {}  
  25.       
  26.     /** 
  27.      * 获取内容 
  28.      * @param configFile 
  29.      * @param property 
  30.      * @return 
  31.      */  
  32.     public static String get(String configFile, String property) {     
  33.         if(!configMap.containsKey(configFile)) {     
  34.            initor.initConfig(configFile);  
  35.         }  
  36.         PropertiesConfiguration config = (PropertiesConfiguration) configMap.get(configFile);  
  37.         String value = config.getString(property);  
  38.         //TODO LOG  
  39.         return value;     
  40.     }     
  41.       
  42.     /** 
  43.      * 载入配置文件,初始化后加入map 
  44.      * @param configFile 
  45.      */  
  46.     private synchronized void initConfig(String configFile) {      
  47.         try {  
  48.             PropertiesConfiguration config = new PropertiesConfiguration(configFile);  
  49.             configMap.put(configFile, config);  
  50.               
  51.         } catch (ConfigurationException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }     
  55. }  


2.调用方法

 

 

[java] view plain copy
 
 print?
    1. configUtil.get("/common/velocity.properties", "input.encoding");  
posted @ 2017-08-10 20:22  小虾米的java梦  阅读(9557)  评论(0编辑  收藏  举报