创建一个Utils类,
生命对象
private TestMqProperties mqProperties ;
路径:
private static final String URL="/testresources.properties";
private Properties properties;
public TestMqProperties(){
mqProperties=null;
init();
}
//初始化并且加载配置文件。
private void init() {
if (properties==null)
{
properties=new Properties();
}
try {
properties.load(gewInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 把配置文件作为文件流读取
* @return
*/
private InputStream gewInputStream() {
return this.getClass().getResourceAsStream(URL);
}
//得到实例对象
public static TestMqProperties getInstance(){
return new TestMqProperties();
}
public String getProperties(String name){
return properties.getProperty(name);
}
配置文件的内容:
main方法测试:
public static void main(String[] args) {
System.err.println(TestMqProperties.getInstance().getProperties("mq.host"));
}