[英].properties as final static variable
I've got a .properties in my java project, that is updated by shell script. 我的Java项目中有一个.properties,由shell脚本更新。 I want to get this properties values and use it as final static variable. 我想获取此属性值并将其用作最终的静态变量。 Here is my .properties code 这是我的.properties代码
WEBURL=http://popgom.fr
NODEURL=http://192.168.2.30:5555/wd/hub
I know I can get and use my .properties using this : 我知道我可以使用以下方法获取和使用我的.properties:
Properties prop = new Properties();
InputStream input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
String urlnode = prop.getProperty("NODEURL");
What I want to do is to get this String in every class of my project, without adding the code bellow in every classes. 我要做的是在项目的每个类中获取此String,而无需在每个类中添加以下代码。
How can I do this ? 我怎样才能做到这一点 ? I've try to do an Interface, without success. 我尝试做一个接口,但没有成功。
Any of you have an idea ? 你们有个主意吗?
Thanks for help 感谢帮助
you can do it using Singleton pattern: 您可以使用Singleton模式进行操作:
Example: 例:
public class MyProperties{
private static MyProperties instance = null;
private Properties prop;
private MyProperties() {
InputStream input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
}
public static MyProperties getInstance() {
if(instance == null) {
instance = new MyProperties();
}
return instance;
}
public Properties getProperty() {
return prop;
}
}
You can invoke this code everywhere: 您可以在任何地方调用此代码:
....
....
MyProperties.getInstance().getProperty();
....
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.