1.引入ResourceLoader所属的Jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.5.RELEASE</version> </dependency>
2.properties配置文件
## IP和端口号 wx.basePath = http://192.168.3.63:8080
3.系统常量类
基于ResourceLoader读取Properties配置文件
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import java.io.InputStream; import java.util.Properties; /** * @version V1.0 * @Title: 系统常量 * @ClassName:SysConstant.java * @Description: * @Copyright 2016-2017 - Powered By 研发中心 * @author: FLY * @date:2016年12月13日 下午4:59:18 */ public class SysConstant { private static final Logger log = LoggerFactory.getLogger(SysConstant.class); /** * IP和端口号<或域名> */ public static String WX_BASE_PATH = ""; /** * 配置文件 */ public static Properties p; private static ResourceLoader resourceLoader = new DefaultResourceLoader(); /** * 属性文件加载对象 */ // private static PropertiesLoader propertiesLoader = new // PropertiesLoader("resource/config.properties"); static { InputStream in = null; try { Resource resource = resourceLoader.getResource("config/cloudpayment.properties"); in = resource.getInputStream(); // in = new BufferedInputStream(newFileInputStream("resource/config.properties")); p = new Properties(); p.load(in); /**IP和端口号*/ WX_BASE_PATH = p.getProperty("wx.basePath").trim(); } catch (Exception e) { log.error("读取配置文件失败", e); } } public static void main(String[] args) { System.out.println("WX_THIRDSERVICE_FORSYS_SERVICE:"+WX_THIRDSERVICE_FORSYS_SERVICE); System.out.println("DL_CLOUD_APIURL:"+p.getProperty("dl.cloud.apiurl").trim()); } }
4.使用
String getUserInfoURL = SysConstant.WX_BASE_PATH;