public static List<String> hosts = new ArrayList<String>();
static Properties pros= new Properties();
static{
try {
pros.load(Content.class.getResourceAsStream("/main_config.properties"));
isDebug = Boolean.valueOf(pros.getProperty("debug"));
if(!isDebug){
host=pros.getProperty("host");
//3d加密key
triple_des_key=pros.getProperty("triple_des_key");
//短信
sms_ip=pros.getProperty("sms_ip");
sms_port=Integer.valueOf(pros.getProperty("sms_port"));
isAbsolute = Boolean.valueOf(pros.getProperty("isAbsolute"));
hosts = initClusters();
//图片
article_img_path=pros.getProperty("article_img_path");
coupon_img_path=pros.getProperty("coupon_img_path");
couponold_img_path=pros.getProperty("couponold_img_path");
slide_img_path=pros.getProperty("slide_img_path");
//路径
filePath=pros.getProperty("filePath");
couponold_filePath=pros.getProperty("couponold_filePath");
coupon_filePath=pros.getProperty("coupon_filePath");
//阿里二维码支付
alipay_partner = pros.getProperty("alipay_partner");
alipay_key = pros.getProperty("alipay_key");
alipay_seller_email = pros.getProperty("alipay_seller_email");
}else{
host=pros.getProperty("debug_host");
triple_des_key=pros.getProperty("debug_triple_des_key");
//短信
sms_ip=pros.getProperty("debug_sms_ip");
sms_port=Integer.valueOf(pros.getProperty("debug_sms_port"));
isAbsolute = Boolean.valueOf(pros.getProperty("debug_isAbsolute"));
hosts = debug_initClusters();
//图片
article_img_path=pros.getProperty("debug_article_img_path");
coupon_img_path=pros.getProperty("debug_coupon_img_path");
couponold_img_path=pros.getProperty("debug_couponold_img_path");
slide_img_path=pros.getProperty("debug_slide_img_path");
//路径
filePath=pros.getProperty("debug_filePath");
couponold_filePath=pros.getProperty("debug_couponold_filePath");
coupon_filePath=pros.getProperty("debug_coupon_filePath");
alipay_partner = pros.getProperty("debug_alipay_partner");
alipay_key = pros.getProperty("debug_alipay_key");
alipay_seller_email = pros.getProperty("debug_alipay_seller_email");
}
} catch (IOException e) {
e.printStackTrace();
}
}
总结:
首先把名为main_config.properties的配置文件放在src下即可,配置文件里的数据样式为name=value。
例如:isAbsolute=true
sms_port=8090
在项目当前类中引入Properties类,Properties pros = new Properties();
pros.load(当前类.class.getResourseAsStream("/main_config.properties"));
当然有时候我们需要在项目上线之前先使用测试环境的配置文件。这时可以巧妙地在配置文件中定义一个变量(变量名字随便起)如 isDebug ,让其值为true或者false,当值为true时,可以加载测试的配置数据,为false时加载正式的数据。其作用相当于一个开关。
紧接着就可以随心所欲的获取配置文件里的数据值了。如: String sms_port = pros.getProperty("sms_port");
..........................................
然后就没有然后了,大功告成!