纯代码
第一步:配置好配置文件:
配置文件置于 框架的 src 下,命名为 application.properties
内容类似于:
#oracle version database settings
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:space
#jdbc.username=space
#jdbc.password=space
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
###æ¬åºç¨æ°æ®åº########################################################################
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/patientfriends?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate settings
hibernate.show_sql=true
hibernate.format_sql=false
#dbcp settings
dbcp.maxIdle=5
dbcp.maxActive=40
里面存放框架中所有应该提取出的参数信息,比如:数据库连接信息,定时器时间戳,Email相关参数信息,图片宽高参数,URl,用户名密码等等。
public class ConfigUtil {
private static PropertiesConfiguration config=null;
static{
try{
//config = new PropertiesConfiguration("app.properties");
//config.setEncoding("UTF-8");
//config.setHeader(header)
config = new PropertiesConfiguration();
config.setEncoding("UTF-8");
config.load("application.properties");
}catch(Exception ex){
}
}
public static int getIntValue(String key){
int reInt = 1;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reInt = config.getInt(key);
}catch(Exception ex){
ex.fillInStackTrace();
reInt = 0;
}
return reInt;
}
public static Long getLongValue(String key) {
Long reLong = 1l;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reLong = config.getLong(key);
}catch(Exception ex){
ex.fillInStackTrace();
reLong = 0l;
}
return reLong;
}
public static double getDoubleValue(String key){
double reDouble = 1.0;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reDouble = config.getDouble(key);
}catch(Exception ex){
ex.fillInStackTrace();
reDouble =1.0;
}
return reDouble;
}
public static String getStringValue(String key){
String str = "";
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
str = config.getString(key);
}catch(Exception ex){
ex.fillInStackTrace();
str = "";
}
return str;
}
public static Boolean getBooleanValue(String key) {
Boolean flag = false;
try{
flag = config.getBoolean(key);
}catch(Exception ex){
ex.fillInStackTrace();
}
return flag;
}
public static void save(String key,Object o){
config.setProperty(key, o);
try{
//config.save("score.properties");
config.save("application.properties");
//config = new PropertiesConfiguration("app.properties");
config = new PropertiesConfiguration();
config.setEncoding("UTF-8");
config.load("application.properties");
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
System.out.println("ewew");
System.out.println(getIntValue("vip.money"));
//ConfigUtil.save("rmb.money", 20);
//ConfigUtil.save("month.live", "123334");
System.out.println(ConfigUtil.getStringValue("month.live1"));
//System.out.println(getStringValue("month.live"));
}
// 读取配置文件
private static ResourceBundle cache = null;
static {
try {
cache = ResourceBundle.getBundle("application");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
/**
* 功能描述:获取配置文件参数值
*
* @param str(参数KEY值)
* @return
*/
public static String getValue(String str) {
String s = cache.getString(str);
try {
s = new String(s.getBytes("ISO8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}
/**
* 功能描述:获取指定配置文件参数的值
*
* @param strPropertiesFile(配置文件名称)
* @param strItem(参数名称)
* @return
*/
public String getPropertiesValue(String strPropertiesFile, String strItem) {
String strItemValue = "";
ResourceBundle resources1 = null;
try {
resources1 = ResourceBundle.getBundle(strPropertiesFile);
strItemValue = resources1.getString(strItem);
} catch (MissingResourceException e) {
System.out.println("ConfigInfo.getPropertiesValue error:"
+ e.getMessage());
}
return strItemValue;
}
}