注册 X
提交 注:点击提交后系统会发送邮件到邮箱验证!(仅支持中国大陆邮箱)
我已阅读并同意 服务条款
首页 > IT技术笔记 > 查看笔记

springboot启动时保存数据到内存中,不使用redis

就一个@PostConstruct注解:

代码在后面

说明:

  1. 启动后访问http://localhost:8848/code,可见控制台输出codeMap中的值

  2. 新建一个只有key和value两个字段的表code



获取缓存值:

System.out.println(CodeCache.codeMap.get("name"));



	        
@Component
public class CodeCache {
	public static Map<String, String> codeMap = new HashMap<String, String>();
	
	@Autowired
	private ICodeService codeService;
	
	@PostConstruct
	public void init(){
		System.out.println("系统启动中。。。加载codeMap");
		List<Code> codeList = codeService.selectAll();
		for (Code code : codeList) {
			codeMap.put(code.getKey(), code.getValue());
		}
	}
	
	@PreDestroy
    public void destroy(){
        System.out.println("系统运行结束");
    }
}			
	      

 打赏        分享



评论

邮箱: 昵称:
\n