@PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记
@PostConstruct 和@PostConstruct 注解
从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解(Annotion);@PostConstruct和@PreDestroy。这两个注解被用来修饰一个非静态的void()方法 。写法有如下两种方式:
@PostConstruct
Public void someMethod() {}
或者
public @PostConstruct void someMethod(){}
被@PostConstruct修饰的方法会在服务器加载Servle的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。PreDestroy()方法在destroy()方法执行执行之后执行。
ehcache 缓存 执行过程
加载servlet时,
通过initCache方法读取缓存配置文件来构建缓存实例
@PostConstruct
void initCache() {
logger = Logger.getLogger(getClass());
if (getCache() == null) {
String clsName = getClass().getSimpleName();
URL url = getClass().getResource(
SystemGlobals.getValue('/xx/ehcache.xml'));
CacheManager manager = CacheManager.create(url);
Cache c = manager.getCache(clsName);
setCache(c);
}
if (logger.isDebugEnabled()) {
logger.debug("初始化:" + getCache());
} }
构建实例后,需一次性加载缓存数据
@PostConstruct
void init() {
if (!isInitCategory) {
initCategories();
isInitCategory = true;
}
if (!isInitBrand) {
initBrands();
isInitBrand = true;
}
} private void initCategories() {
Range<Category> range = categoryDao.select();
categoryCache.putAll(range.getData()); }
public void putAll(Collection<Category> categories) {
List<Category> list = new ArrayList<Category>(categories); Element element = new Element(FQN_ALL, list);
cache.removeAll();
cache.put(element);
}
项目启动后,访问项目,读取缓存数据
private static Cache cache;
private static final String FQN_ALL = "all";
private static final String FQN_KEY = "key";
private static final String FQN_CHILDREN = "children";
private static final String FQN_ROOT = "root";
@SuppressWarnings("unchecked")
public Collection<Category> root() {
String key = FQN_ROOT;
Element element = cache.get(key);
if (element != null) {
return (List<Category>) element.getValue();
}
List<Category> root = new ArrayList<Category>();
List<Category> categories = getCategories();
for (Category category : categories) {
if (category.isRoot()) {
root.add(category);
}
}
element = new Element(key, root);
cache.put(element);
return root;
}
@SuppressWarnings("unchecked")
private List<Category> getCategories() {
Element element = cache.get(FQN_ALL);
if (element != null) {
return (List<Category>) element.getValue();
}
return new ArrayList<Category>(0);
}
@PostConstruct和@PostConstruct 注解 及ehcache 缓存 执行过程 小记的更多相关文章
- Java开发之@PostConstruct和@PreConstruct注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- @PostConstruct和@PreDestroy注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- Java开发之@PostConstruct和@PreDestroy注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...
- 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...
- Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存
一 基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 --> <bean cl ...
- SpringBoot 缓存注解 与EhCache的使用
在SpringBoot工程中配置EhCache缓存 1.在src/main/resources下新建ehcache.xml文件 eternal=true //缓存永久有效,false相反 maxEle ...
- Spring自定义缓存管理及配置Ehcache缓存
spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...
随机推荐
- 使用openshit在ubuntu14.04下一键部署openstack(juno版本)
一.基本介绍 本实验是在vmware workstation上虚拟机ubuntu14.04(64bit,desktop)上部署openstack(Juno版本).采用的工具是openshit.open ...
- hive mysql 初始化
原文链接:https://juejin.im/post/59c3f8f75188255be81f91d9#heading-17 Apache Hive-2.3.0 快速搭建与使用 Hive 简介 Hi ...
- CentOS 6.5下Redis安装测试
NoSQL之Redis - CentOS 6.5安装测试 1.下载redis 可以在线安装或者下载 redis ①在线安装前需要检测是否存在rpm包不存在的话查看yum在线是否存在rpm包不存在的话就 ...
- python文件操作 二
问题:在传输的时候对方不知道是以什么格式传给用户,传来传去彼此都忘了什么格式: 在编码的时候:utf8:是3个字符,gbk是2个字符,每个字符数字对应的字符,一段2进制串,如果10个字节全部都是中文, ...
- jquery mobile两个页面以及源码(登录与注册) 转
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- defer和async的详细区别
看过javascript高级程序设计的人,在javascript高级程序设计里,应该看到了介绍了有关defer和async的区别,可是比较浅显,而且也说得不是很清楚.下面我们来通过图片来详细了解下df ...
- NumPy 从数值范围创建数组
NumPy 从数值范围创建数组 这一章节我们将学习如何从数值范围创建数组. numpy.arange numpy 包中的使用 arange 函数创建数值范围并返回 ndarray 对象,函数格式如下: ...
- 有关vim的一些命令
所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...
- 206. Reverse Linked List (List)
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- Shell教程 之echo命令
1.显示普通字符串 这里的双引号完全可以省略,以下命令效果一致: echo "传递参数实例!" echo 传递参数实例! 2.显示转义字符 echo "\"传递 ...