50,752
社区成员
发帖
与我相关
我的任务
分享
@Service
public class DemoService implements IDemoService {
@Override
public Object getDictionary() {
//get records from database
Object records = null;
return records;
}
}
controller
@Controller
public class DemoController {
@Autowired
DemoService service;
@RequestMapping("/getDictionary")
public Object get() {
Object dict = Global.builder().getOrDefault("dictionary", null);
if (dict == null) {
dict = service.getDictionary();
Global.builder().put("dictionary", dict);
}
return dict;
}
}
Global
public class Global extends HashMap<String, Object> {
private static class Holder {
private static final Global cache = new Global();
public static Global instance() {
return cache;
}
}
public static Global builder() {
return Global.Holder.instance();
}
}