@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. the type java.io.ObjectInputStream cannot be resolved. It is indirectly......

    问题的原因: 配置tomcat7.0的时候自己设置了jre的版本1.8,而没有用myeclipse10自带的jre1.6,导致了出现了差错! 两种解决的办法: 1.点击windows--->pr ...

  2. 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理

    1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...

  3. 1.5.6、CDH 搭建Hadoop在安装之前(定制安装解决方案---使用Cloudera Manager模板创建CDH群集)

    使用Cloudera Manager模板创建CDH群集 您可以通过从Cloudera Manager管理的现有CDH群集导出群集模板来创建新的CDH群集.然后,您可以修改模板并使用它在新的主机集上创建 ...

  4. JMeter学习(三)元件的作用域与执行顺序(转载)

    转载自 http://www.cnblogs.com/yangxia-test 1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它 ...

  5. Oracle 入门

    一.安装Oracle 11g 服务端 服务端安装教程:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 二.安装客户端 客 ...

  6. Java 并发AQS

    转载出处:http://www.cnblogs.com/waterystone/ 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQu ...

  7. MVC中html编码与否

    MVCHtmlString可以不进行编码. 例子:string s=‘<a style="color:red">链接</a>’: 1.@s,直接输出< ...

  8. POJ-1426.Findthemultiple.(BFS)

    一开始模拟了一波大数取余结果超时了,最后改成long long过了emmm... 本题大意:给出一个200以内的数n,让你找出一个m使得m % n == 0,要求m只有1和0组成. 本题思路:BFS模 ...

  9. JavaScript各种继承方式(六):寄生组合继承

    一 原理 用寄生继承来改造组合继承. function Fruit(name){ this.name = name; } Fruit.prototype.eat = function(){ conso ...

  10. Mac Sublime Text3 如何安装插件

    1.打开sublime text3后按快捷键control+`后下面会出来东西,然后输入如下命令. import urllib.request,os; pf = 'Package Control.su ...