我需要您提供有关从jersey Web服务中检索存储在java属性文件中的配置数据的最佳方法的建议,以便在这些Web服务调用的几个DAO类中使用它们。
我实施的解决方案如下:
这是一个好方法吗?如果没有,你能建议我另一个解决方案吗?
答案 0 :(得分:0)
而不是将单个属性作为属性放入上下文中,将属性对象放在上下文中,并从该prop对象获取所需的属性。
答案 1 :(得分:0)
为什么不使用Singleton类来读取属性文件, 然后通过implmenting ServletContextListener @ contextInitialized方法创建一个applicationLister并调用你的类的getInstance(这一步不是必需的,所以你可以离开它,它只是在app容器的开头实例化单例),
在你的整个项目中,只需调用satic方法YouClass.getInstance()并访问你的属性。
以示例:
public class MyPropertieFileReader {
private static MyPropertieFileReader instance = null;
private Properties properties;
protected MyPropertieFileReader() throws IOException{
properties = new Properties();
properties.load(getClass().getResourceAsStream("path-to-property-file.properties"));
}
public static MyPropertieFileReader getInstance() {
if(instance == null) {
try {
instance = new TestDataProperties();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return instance;
}
public String getProperty(String key) {
return properties.getProperty(key);
}
}
在你的WS中,只需致电
MyPropertieFileReader.getInstance().getProperty("property-name");
希望这会有所帮助。