[英]How to load properties from config.properties in multithreaded environment in JAVA
我有程序从文件config.properties加载主类属性,如下所示:
public class DirectoryWatcher {
public static String FOLDER = null;
Properties prop = new Properties();
prop.load(new FileInputStream(new File(configPath)));
FOLDER = prop.getProperty("FOLDER");
}
许多线程需要FOLDER,因为我将它设置为公共静态 ,因此线程可以使用它。
我不喜欢这种编程,我正在寻找一些最佳实践实现。
你能给我一些更好的建议吗? 谢谢。
对我来说,这就足够了
public class DirecoryWatcher{
private static String FOLDER;
public static synchronized getFolder(){
if(FOLDER == null){
// FOLDER = your loading code
}
return FOLDER;
}
}
确保将从文件中读取的值分配给静态字段,因此请确保只读取一次。
此外,同步方法是访问资源的一种很好的做法,对于这种情况,它并不是完全必需的,因为您只是在阅读文件。
您还可以使此方法可扩展,以读取作为参数给出的任何属性。 为了清晰起见,我硬编码了FOLDER。
public class DirectoryWatcher{
private static Map<String,String> properties = new HashMap<String,String>();
public static synchronized getValueFor(String prop){
String result = null;
if( !properties.keySet().contains(prop)){
result = // your loading code
properties.put(prop, result);
}else{
result = properties.get(prop);
}
return result;
}
}
此代码将为您提供线程安全性并支持任何给定数量的属性。 它还增强了代码的封装,你可以为它添加一些逻辑(你不只是暴露文件的内容)。
此外,在这种情况下,在首次需要属性之前不会加载属性。 如果从未使用过属性,则不会读取它。 这可以提高内存使用率,不会浪费内存在您不需要的值上。
另一个需要考虑的重要事项是,通过此实现,您的属性加载器类可以非常轻松地处理错误和异常。 使用另一种方法,您将处理问题的责任委托给请求该属性的对象。
你可以做到最终:
private static final Properties prop = new Properties();
public static final String FOLDER;
static {
try {
prop.load(new FileInputStream(new File(configPath)));
} catch (IOException ex) {
//outch => log and exit?
}
FOLDER = prop.getProperty("FOLDER");
}
这将确保从任何线程都可以看到它。 如果您拥有多个属性,则还可以使用此示例使用枚举并且是线程安全的。
也许你可以使用Singleton模式来解决这个问题?
如果您有大量要加载的信息,也可以对文件夹使用延迟加载。
你可以像这样定义一个简单的属性文件阅读器
public class LoadDataFromPropertiesFile {
public final static Properties loadPropertiesFile(String fileName) {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream inStream=null;
try {
inStream = loader.getResourceAsStream(fileName);
if (inStream == null) {
throw new RuntimeException("Couldn't find " + fileName + "in class path");
}
Properties prop = new Properties();
prop.load(inStream);
return prop;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
特定属性文件可以如下所示。
public class DirectoryWatcher {
public static final Properties directoryWatcher;
static {
directoryWatcher = LoadDataFromPropertiesFile.loadPropertiesFile("config.properties");
}
}
您可以定义为ENUM类型的属性...您可以在此处列出您的属性
public enum DirectoryProperties {
FOLDER("FOLDER","Directory Type Folder"),
IMAGE("IMG","Image File");
;
DirectoryProperties(String code, String description) {
this.code = code;
this.description = description;
}
public String getCode() {
return code;
}
public String getDescription() {
return description;
}
private String code;
private String description;
}
您可以在任何线程中使用您的属性。 像这样
DirectoryWatcher.directoryWatcher.getProperty(DirectoryProperties.FOLDER.getCode());
您可以根据需要使用说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.