篇四:配置文件与系统常量
导语:properties配置文件的两种读取方式(xml读取、Java读取),以及系统常量的定义。Enum、Class、interface、properties四种不同类型的文件常用于不停场合的存储系统常量;
Enum:定义具有相同类型的一批属性对象,不能用于定义系统常量;
Class:定义系统常量,public static final
interface:和Class一样定义系统常量
properties:定义指定服务的参数值,规范化可以直接在不同项目之间复制使用
一、properties的使用
1、以jdbc.properties为例
################## MySQL ################## #db.dbType=mysql ----- home jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.8.85:3306/basic?useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&rewriteBatchedStatements=true jdbc.username=root jdbc.password=root
2、在xml中使用,在location中配置properties的路径
<!-- 加载properties --> <context:property-placeholder location="classpath*:com/config/properties/*.properties"/>
<!--使用-->
<property name="driverClassName" value="${jdbc.driver}" />
3、在Java中使用:ResourceBundle,支持国际化
国际化:http://www.cnblogs.com/fjdingsd/p/5143551.html
路径:
1、统一路径下,直接使用文件名;
2、不同路径,使用绝对路径,建议直接放在"src/main/resources"。
String模板:MessageFormat
在properties中定义String型的模板,动态信息部分用{0}、{1}、{2}代替,然后用MessageFormat,使用指定的对象数组替换;
package com.guduo.common.utils; import java.text.MessageFormat; import java.util.HashMap; import java.util.Map; import java.util.ResourceBundle; /** * 资源文件工具类 * @author luck * */ public class ResourceUtils { private ResourceBundle resourceBundle; private ResourceUtils(String resource) { resourceBundle = ResourceBundle.getBundle(resource); } /** * 获取资源 * @param resource 资源 * @return 解析 */ public static ResourceUtils getResource(String resource) { return new ResourceUtils(resource); } /** * 根据key取得value * @param key 键值 * @param args value中参数序列,参数:{0},{1}...,{n} * @return */ public String getValue(String key, Object... args) { String temp = resourceBundle.getString(key); return MessageFormat.format(temp, args); } /** * 获取所有资源的Map表示 * @return 资源Map */ public Map<String, String> getMap() { Map<String, String> map = new HashMap<String, String>(); for(String key: resourceBundle.keySet()) { map.put(key, resourceBundle.getString(key)); } return map; }
/**
* 获取所有资源的Map表示
* @return 资源Map
*/
public Map<String, String> getMap(String resource) {
ResourceBundle file = getResource(resource);
Map<String, String> map = new HashMap<String, String>();
for(String key: file.keySet()) {
map.put(key, resourceBundle.getString(key));
}
return map;
}
}
MessageFormat模式(主要部分):
FormatElement:
{ ArgumentIndex }:是从0开始的入参位置索引。
{ ArgumentIndex , FormatType }
{ ArgumentIndex , FormatType , FormatStyle }
FormatType: :指定使用不同的Format子类对入参进行格式化处理。值范围如下:
number:调用NumberFormat进行格式化
date:调用DateFormat进行格式化
time:调用DateFormat进行格式化
choice:调用ChoiceFormat进行格式化
FormatStyle::设置FormatType中使用的格式化样式。值范围如下:
short
medium
long
full
integer
currency
percent
SubformatPattern (子格式模式,形如#.##)
二、Class 、interface定义系统常量
package com.config.interfaces; /** * 系统参数配置 * @author liuguangping * */ public interface ConfigureConstant { public static final long maxFileSize = 1024*1024*1024; //文件最大上传大小 public static final String ftpBasicPath = "http://192.168.1.61/"; //Ftp上传基本路径 public static final String localFileBasicPath = "D:/ProjectFile/"; //本地存储路径 }
三、Enum定义同类型常量
package com.config.enums; public enum DemoEnum { DEMO_ONE(1,"第一个"), DEMO_TWO(2,"第二个"), DEMO_THREE(3,"第三个"), DEMO_THIRD(4,"第四个"), DEMO_FIVE(5,"第五个"); public int value; public String desc; /** * 根据value获取对应的desc * @param value * @return */ public Object getDesc(int value) { DemoEnum[] demos = DemoEnum.values(); for(DemoEnum demo:demos) { if(value==demo.getValue()) { return demo.getDesc(); } } return value; } private DemoEnum(int value,String desc) { this.value = value; this.desc = desc; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }

浙公网安备 33010602011771号