@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自带的缓存管理有一个总体的认识. 这篇文 ...
随机推荐
- 微信小程序之富文本解析
亲身体验 wxparse 是个坑,弃之不用 微信小程序的 <rich-text>标签挺好用的 用法如下: 1.wxml页面 <rich-text nodes="{{node ...
- 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)
Impala使用的端口 Impala使用下表中列出的TCP端口.在部署Impala之前,请确保在每个系统上打开这些端口. Component Service Port Access Requireme ...
- onMouseOver&onMouseOut vs onMouseEnter&onMouseLeave
[onMouseOver&onMouseOut vs onMouseEnter&onMouseLeave] 1.onmouseleave.onmouseenter,鼠标进入到指定元素区 ...
- Emulating private methods with closures
[Emulating private methods with closures] JavaScript does not provide a native way of doing this, bu ...
- 修复回写PR时到料日期重复扣减检验周期的问题:
问题描述: 修复回写PR时到料日期重复扣减检验周期的问题:系统回写的外购半成品PR交货日期未按采购周期回写,从8-10日开始均于10天交期回写,例以下9-5日今天回写的PR,采购周期12天,结果回写到 ...
- PO接口表
---物料主数据接口 SELECT count(*)PO_MARA FROM STG.PO_MARA; SELECT count(*)PO_MARC FROM STG.PO_MARC; SELECT ...
- msf客户端渗透(三):提权、图形化payload
对普通权限session提权 生成一个木马 开启Apache服务 将木马上传网页 被攻击者从这个网页上获取到这个木马 攻击者开启msf侦听 当被攻击者双击这个木马文件时 攻击者就获取到一个sessio ...
- java.net.UnknownHostException: www.terracotta.org
异常日志: java.net.UnknownHostException: www.terracotta.org at java.net.PlainSocketImpl.connect(PlainSoc ...
- day17 正则表达式 re模块和hashlib模块
今日内容 1. re&正则表达式(*****) 注:不要将自定义文件命名为re import re re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表 ...
- UA判断跳转
<script type="text/javascript"> UA = navigator.userAgent.toLowerCase(); url = window ...