使用自定义属性文件的方法

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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值