轻松读取项目中properties文件的方式

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * 读取ipconfig.properties
 * @data 2017-12-25
 * @author LQ
 */
public class IpConfigUtil {


    private static Properties configfile = null;


    static {
        InputStream in = IpConfigUtil.class.getClassLoader()
                .getResourceAsStream("ipconfig.properties");
        configfile = new Properties();
        try {
            configfile.load(in);
            in.close();
        } catch (IOException e) {
            System.out.println("No config.properties defined error");
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                in = null;
            }
        }
    }


    public String getPropertiesByKey(String _key) {
        return configfile.getProperty(_key);
    }


}


ipconfig.properties内空如下:

#ipconfig localhost
#socketio client io
SOCKETIO_CLIENT=http://localhost:9092
#redis server ip
REDIS_SERVER=192.168.xx.xx
#get shaggy url 
SHAGGY_GET_URL=http://192.168.xx.xxx:9090/
#get postadd
POSTADD=http://192.168.xx.xx:8080/projectname/analyzeShaggy/analyzeJson.do
#socketio-netty server ip
SOCKETIO_SERVER=localhost

调用时如下:

	/**
	 * 获取配置中 获取 socketio client ip ; shaggy geturl; shaggy postadd
	 * @return
	 */
	@RequestMapping(value = "/getIpConfig.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, String> getIpConfig1(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("socket_client", new IpConfigUtil().getPropertiesByKey("SOCKETIO_CLIENT"));
		map.put("shaggy_get_url", new IpConfigUtil().getPropertiesByKey("SHAGGY_GET_URL"));
		map.put("postadd", new IpConfigUtil().getPropertiesByKey("POSTADD"));
		System.out.println(map.toString());
		return map;
	}





猜你喜欢

转载自blog.csdn.net/liq816/article/details/78909269
今日推荐