PropertiesConfiguration的用法
PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法。
使用org.apache.commons.configuration2中提供的工具来读取属性文件
1.创建Java工程
2.引入所需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。
直接上代码:
- public class Config {
- private static PropertiesConfiguration config = null;
- static {
- try {
- config = new PropertiesConfiguration(
- "config.properties");
- } catch (ConfigurationException e) {
- e.printStackTrace();
- }
- }
- public static String getString(String key) {
- return config.getString(key);
- }
- public static String getString(String key, String def) {
- return config.getString(key, def);
- }
- public static int getInt(String key) {
- return config.getInt(key);
- }
- public static int getInt(String key, int def) {
- return config.getInt(key, def);
- }
- public static long getLong(String key) {
- return config.getLong(key);
- }
- public static long getLong(String key, long def) {
- return config.getLong(key, def);
- }
- public static float getFloat(String key) {
- return config.getFloat(key);
- }
- public static float getFloat(String key, float def) {
- return config.getFloat(key, def);
- }
- public static double getDouble(String key) {
- return config.getDouble(key);
- }
- public static double getDouble(String key, double def) {
- return config.getDouble(key, def);
- }
- public static boolean getBoolean(String key) {
- return config.getBoolean(key);
- }
- public static boolean getBoolean(String key, boolean def) {
- return config.getBoolean(key, def);
- }
- public static String[] getStringArray(String key) {
- return config.getStringArray(key);
- }
- @SuppressWarnings("unchecked")
- public static List getList(String key) {
- return config.getList(key);
- }
- @SuppressWarnings("unchecked")
- public static List getList(String key, List def) {
- return config.getList(key, def);
- }
- public static void setProperty(String key, Object value) {
- config.setProperty(key, value);
- }
- }
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配置如下:
- <dependency>
- <groupId>commons-configuration</groupId>
- <artifactId>commons-configuration</artifactId>
- <version>1.9</version>
- </dependency>
- <!-- commons-configuration 自动加载的是2.1的版本,编译时会报错,所以再加上这个 -->
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
common-lang这个包要用新版的,如果不写这个依赖,commons-configuration会下载一个2.1旧版,导致编译出错
2.java代码:
- PropertiesConfiguration config = new PropertiesConfiguration(“/database.properties”);
- String userName = config.getString("name");
除了getString()方法外,还有getBoolean,getDouble,getInteger等不同返回类型的方法可以调用。
进阶用法:
一个项目有会有多个配置文件,这时有个统一的配置文件管理类就很有必要了,我写了一个简单的,大家可以参考下,有不妥的用法也请指出来
1.java类
- package com.xxx.xxx.util;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- /**
- * <p>
- * 读取配置文件类
- * </p>
- * <p>
- * 根据配置文件名和属性key返回属性内容,configUtil.get(configFile, property);
- * </p>
- * @author shengzhi.rensz
- *
- */
- public class configUtil {
- private static configUtil initor = new configUtil();
- private static Map<String, Object> configMap = new HashMap<String, Object>();
- private configUtil() {}
- /**
- * 获取内容
- * @param configFile
- * @param property
- * @return
- */
- public static String get(String configFile, String property) {
- if(!configMap.containsKey(configFile)) {
- initor.initConfig(configFile);
- }
- PropertiesConfiguration config = (PropertiesConfiguration) configMap.get(configFile);
- String value = config.getString(property);
- //TODO LOG
- return value;
- }
- /**
- * 载入配置文件,初始化后加入map
- * @param configFile
- */
- private synchronized void initConfig(String configFile) {
- try {
- PropertiesConfiguration config = new PropertiesConfiguration(configFile);
- configMap.put(configFile, config);
- } catch (ConfigurationException e) {
- e.printStackTrace();
- }
- }
- }
2.调用方法
- configUtil.get("/common/velocity.properties", "input.encoding");
PropertiesConfiguration的用法的更多相关文章
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
- 【JavaScript】innerHTML、innerText和outerHTML的用法区别
用法: <div id="test"> <span style="color:red">test1</span> tes ...
- chattr用法
[root@localhost tmp]# umask 0022 一.chattr用法 1.创建空文件attrtest,然后删除,提示无法删除,因为有隐藏文件 [root@localhost tmp] ...
- 萌新笔记——vim命令“=”、“d”、“y”的用法(结合光标移动命令,一些场合会非常方便)
vim有许多命令,网上搜有一堆贴子.文章列举出各种功能的命令. 对于"="."d"."y",我在无意中发现了它们所具有的相同的一些用法,先举 ...
随机推荐
- 微信公众号--JS-SDK
JS-SDK 微信JS-SDK是微信公众平台 面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以 ...
- YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)
题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...
- 1 http协议
1.四层模型 + 2.socket 3.http协议 4. HTTP请求 跟踪了新浪的首页,我们来总结一下HTTP请求的流程: 3.1.1 步骤1:浏览器首先向服务器发送HTTP请求,请求包括: 方法 ...
- sql插入查询出的数据,主键递增
INSERT INTO C_DPRECORD SELECT (SEQ_C_DPRECORD.NEXTVAL ) AS ID, DEV_ID, DEV_CHNNUM, DEV_NAME, DEV_CHN ...
- spring boot 入门3 如何在springboot 上使用AOP
Aop是spring的两大核心之一 那么如何在springboot中采用注解的形式实现aop那? 1)首先我们定义一个相关功能的切面类 并 采用@Aspect 注解来声明当前类为切面 同时采用@Com ...
- linux shell中读写操作mysql数据库
本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...
- Apache 配置说明
ServerRoot ServerRoot: The top of the directory tree under which the server's configuration, error, ...
- 最火的.NET开源项目[转]
综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...
- Java并发(多线程)
一.多线程的基本概念 1.什么是进程.多进程有什么作用? 大家都使用计算机,当我们打开某一个软件的时候,其实就是启动了一个进程,可以打开任务管理器看看,我们打开的每一个软件,都是一个进程,在同一个操作 ...
- 【性能调优】一次关于慢查询及FGC频繁的调优经历
以下来分享一个关于MySQL数据库慢查询和FGC频繁的性能案例. 一.系统架构 一个简单的dubbo服务,服务提供者提供接口,并且提供接口的实现,提供方注册服务到Zookeeper注册中心,然后消费者 ...