@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 缓存 执行过程 小记的更多相关文章

  1. Java开发之@PostConstruct和@PreConstruct注解

    从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...

  2. @PostConstruct和@PreDestroy注解

    从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...

  3. Java开发之@PostConstruct和@PreDestroy注解

    从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...

  4. spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除

    转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...

  5. spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...

  6. 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除

    转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...

  7. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  8. SpringBoot 缓存注解 与EhCache的使用

    在SpringBoot工程中配置EhCache缓存 1.在src/main/resources下新建ehcache.xml文件 eternal=true //缓存永久有效,false相反 maxEle ...

  9. Spring自定义缓存管理及配置Ehcache缓存

    spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...

随机推荐

  1. jvm中堆和栈的区别

    1.前言.    其实jvm能优化的空间不多,最主要的是使用的共享内存不要超过默认的2g或者自己调的参数.但了解一下还是有点意思的,建议面试时还是要看,别学笔者裸奔. 2.区别.   网上说是有5点区 ...

  2. 用Navicat for MySQL 连接 CentOS 6.5

     navicat for mysql windows 端 连接mysql服务器 用SSH通道访问 , 新建连接时配置如下 (两张图解释) 1.配置SSH . 配置常规

  3. 求值器本质--eval&apply

    最近跟着(How to Write a (Lisp) Interpreter (in Python))使用python实现了一个简易的scheme解释器.不得不说使用python这类动态语言实现不要太 ...

  4. thymeleaf 获取sessionid

    参考https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html ${#session.id}

  5. sqlserver 事务嵌套

    参考 https://www.cnblogs.com/JentleWang/p/3654603.html https://blog.csdn.net/tuzhen007/article/details ...

  6. 激活 pycharm

    step1: 在本地 hosts 文件增加一行,windows 路径一般为:C:\Windows\System32\drivers\etc step2: 输入激活码 7SPIY8PDT7-eyJsaW ...

  7. 二叉树中和为某一值的路径(python)

    题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...

  8. Ubuntu 16.04下添加新用户

      某些情况下,Ubuntu 使用useradd 新用户名,在home 文件夹下面看不到新创建的用户文件夹,例如:root@worker:/home/kuku# useradd spark root@ ...

  9. Linux之间配置SSH互信(SSH免密码登录)

    为简化SSH过程,采用证书方式,免去SSH登入时需要输入账号密码的过程,具体操作如下: 一.在SSH服务器所在机器上 1.以root用户登录,更改ssh配置文件 /etc/ssh/sshd_confi ...

  10. spring上下文快速获取方法

    import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContex ...