我怕你在这里混合概念....
属性文件是包含键 - 值对存储配置和运行时阅读纯文本文件。他们与你的问题没有什么共同之处。
无状态和有状态bean又是不同的东西。一个有状态的bean可能有一个内部状态。即使上下文(db-contents等)没有改变,对相同方法的两次调用可能会产生不同的结果。
那么实现缓存的方法是什么?没有内置的实用程序来缓存DB请求。最简单的(但也幼稚)的方式会是这样的
private final Map<String, String> componentCache = new HashMap<>();
public String getComponentMarkup(String componentId, String... parameters){
//Build a key for the parameters. Maybe simple stringconcatenation.
String key=buildKey(componentId, parameters);
if (!componentCache.containsKey(key)){
//Delegate the call to the "old" method which queries the database.
String component = getComponentMarkupFromDb(componentId,parameters);
componentCache.put(key, component);
}
return componentCache.get(key);
}
但底线是,你应该想想为什么你想自己来实现缓存。有很多经过良好测试的解决方案适用于您的问题(spring,hazelcast,etc ...)。 你的问题表示not invented here syndrom ...
http://stackoverflow.com/questions/8292940/how-to-set-session-attribute-in-java? – 2016-05-17 04:32:21