1、读取 myconfig.properties 改为使用包装类,防止当key不存在时通过getString()获取值时抛出异常

This commit is contained in:
fangshunjian
2018-11-23 11:16:51 +08:00
parent fa6f40efb5
commit d26010844a
3 changed files with 55 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import java.util.concurrent.Future;
import nis.nms.util.BaseAction;
import nis.nms.util.ConnectionOracle;
import nis.nms.util.ResourceBundleWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
@@ -44,7 +45,7 @@ public class MailingManagerThread implements Runnable{
}
}
ResourceBundle rb = BaseAction.rb;
ResourceBundleWrapper rb = BaseAction.rb;
String address = rb.getString("email.address");
String userName = rb.getString("email.userName");
String password = rb.getString("email.password");

View File

@@ -249,7 +249,7 @@ public abstract class BaseAction extends ActionSupport implements Serializable
// private CommonService commonService;
private static final long serialVersionUID = 7414962517552053966L;
public static ResourceBundle rb = ResourceBundle.getBundle("myconfig");
public static ResourceBundleWrapper rb = new ResourceBundleWrapper(ResourceBundle.getBundle("myconfig"));
private String downLoadPath;//下载文件路径
@@ -1897,11 +1897,11 @@ public abstract class BaseAction extends ActionSupport implements Serializable
public void setDownLoadPath(String downLoadPath) {
this.downLoadPath = downLoadPath;
}
public static ResourceBundle getRb() {
public static ResourceBundleWrapper getRb() {
return rb;
}
public static void setRb(ResourceBundle rb) {
BaseAction.rb = rb;
BaseAction.rb = new ResourceBundleWrapper(rb);
}
public String getPrevPageUrl() {

View File

@@ -0,0 +1,50 @@
package nis.nms.util;
import java.util.ResourceBundle;
/**
* ResourceBundle包装类
* 解决当配置文件没有key时抛出异常的的问题
* @author fang
*
*/
public class ResourceBundleWrapper {
private ResourceBundle rb;
public ResourceBundleWrapper(ResourceBundle rb) {
this.rb = rb;
}
/**
* 当配置文件中没有配置key时返回 null
* @param key
* @return
*/
public String getString(String key){
return getString(key, null);
}
/**
* 当配置文件中没有key时返回 def 默认值
* @param key
* @param def
* @return
*/
public String getString(String key,String def){
if(rb.containsKey(key)){
return rb.getString(key);
}
return def;
}
public boolean containsKey(String key){
return rb.containsKey(key);
}
public ResourceBundle getRb() {
return rb;
}
public void setRb(ResourceBundle rb) {
this.rb = rb;
}
}