1.在src根目录定义属性文件,application.properties
#pruduct
webi.host=BOE1993:6400
webi.username=bocadmin
webi.password=bocadmin
#test
#webi.host=BOE03131:6400
#webi.username=administrator
#webi.password=accenture03131
webi.auth=secEnterprise
#plan.webservice=http://localhost:8080/bocws/ws/services/PlanService
#plan.webservice=http://10.1.19.70:7001/bocws/ws/services/PlanService
plan.webservice=http://10.1.19.70:10002/bocws/ws/services/PlanService
check.thread=2
2.定义操作类,ApplicationProperties.java
package com.isoftskill.framework.util;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
/**
* @author xiaowang
*
*/
public final class ApplicationProperties {
private static final String resourceName = "/application.properties";
private static Properties property = new Properties();
static {
ApplicationProperties.init();
}
/**
* 初始化
*/
public static void init() {
ApplicationProperties.setProperty(PropertiesLoaderUtils.loadUrlProperties(resourceName));
}
/**
* @return Properties
*/
public static Properties getProperty() {
//System.out.println(property.isEmpty());
if (property == null || property.isEmpty() || property.size() == 0) {
ApplicationProperties.init();
}
return property;
}
public static void setProperty(Properties property) {
ApplicationProperties.property = property;
}
/**
* 获取属性值
* @param key
* @return String
*/
public static String getPropertyValue(String key) {
if (StringUtils.isEmpty(key)) {
return "";
}
return (String) ApplicationProperties.getProperty().get(key);
}
}
3.定义辅助类,PropertiesLoaderUtils.java
package com.isoftskill.framework.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
/**
* @author xiaowang
*
*/
public final class PropertiesLoaderUtils {
/**
* read properties
*
* @param resourceName "/"
* @return Properties
*/
public static Properties loadUrlProperties(String resourceName) {
Properties props = new Properties();
InputStream is = null;
URL url = PropertiesLoaderUtils.class.getResource(resourceName);
URLConnection con;
try {
con = url.openConnection();
con.setUseCaches(false);
is = con.getInputStream();
props.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return props;
}
/**
* read properties
* @param resourceName "/"
* @return Properties
*/
public static Properties loadStreamProperties(String resourceName) {
Properties props = new Properties();
InputStream is = null;
try {
is = PropertiesLoaderUtils.class.getResourceAsStream(resourceName);
props.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
is = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return props;
}
}
4.调用
String msdl = ApplicationProperties.getPropertyValue("plan.webservice");