web工程中怎么启动时,或者第一次调用时,读取一次属性文件,之后找地方存起来。现在每次调用都读取,效率不高。能不能说的详细点,最好给个例子
是用什么语言呢?如果是java语言的话,java web工程可以在tomcate容器启动的时候定义一些监听器,在这里面执行一些必要的初始化操作。
你可以将属性加载操作放在这里,然后将数据存储到内存中,以后直接从内存中获取属性信息就可以了。
/**
* 模板名称映射map集合
*/
private static Map<String,String> templateNameMap=new HashMap<String,String>();
//静态加载配置
static{
Properties pros = new Properties();
try {
pros.load(TeamplateNameMapUtil.class.getResourceAsStream("/properties/templateNameMap.properties"));
if(pros.isEmpty()){
logger.error("load templateNameMap.properties content is null");
}else {
for (Object templateName:pros.keySet()) {
String key=templateName.toString();
Object value=pros.get(templateName);
if (value == null || value.toString().equals("")){
continue;
}else{
String valueStr=value.toString();
templateNameMap.put(key,valueStr);
}
}
}
} catch (IOException e) {
logger.error("load templateNameMap.properties has exception:",e);
}
}
````
类里面写个静态的加载模块,把内容放map里面,每次来取不会重新加载了