Spring MVC属性表
问题描述:
我们使用Spring MVC + Hibernate。Spring MVC属性表
我们在我们的数据库中有查找表。想要如果有反正我可以将值自动加载到一个bean/properties/map,这样我就不必每次都需要访问数据库。
表内容必须在服务器启动期间加载到prop/bean/map。
请帮忙。
答
在@PostConstruct
回调只需加载数据:
@Service
public class ReferenceData {
private Map cached//...
@PostConstruct
public void loadReferenceData() {
//load from database and store in `cached` field
}
}
小心,@PostConstruct
不是事务性的,不能,即使与@Transactional
注解。
另一种方法是利用Hibernate L2缓存和查询缓存。每次从数据库需要数据时加载数据,并让Hibernate内置的缓存层处理它。
是否有任何其他解决方案,如使用启动监听器? – 2012-03-08 02:31:47
@ user1102835你的意思不是'@ PostConstruct'?在Spring中,还有'init-method =''''和'InitializingBean'接口。 – 2012-03-08 07:56:36