@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. mongodb数据到MySQL数据库 的迁移步骤

    废话少说,直接上干货. 1.mongoexport -d shengyang -c testData -f _id,x,name,name1,name2 --type=csv -o new.csv 用 ...

  2. HttpClient之EntityUtils对象

    最近在学习安卓并用thinkphp做后台,为了抵抗自己的烂记性,就在这里记录一下当我从tp后台获取到json串传到安卓客户端所用到的一个方法函数. EntityUtils对象是org.apache.h ...

  3. Controlled Components

    [Controlled Components] In HTML, form elements such as <input>, <textarea>, and <sele ...

  4. Web前端js下载流文件

    前端下载文件大概有以下种: 1)a标签链接下载 <a href="url">点击链接下载</a> 2)表单form提交下载 var form = $(&qu ...

  5. 用photoshop 把视频镜头做成GIF图片

    https://jingyan.baidu.com/article/47a29f2432e113c0142399b0.html

  6. jquery 动态数字滚动

    1.引入jQuery <script src="js/jquery.min.js"></script>2.html <div id="cou ...

  7. 第三章 列表(e)插入排序

  8. MAP使用方法集合

    一.整理: 看到array,就要想到角标. 看到link,就要想到first,last. 看到hash,就要想到hashCode,equals. 看到tree,就要想到两个接口.Comparable, ...

  9. Django使用jsonp和cors解决跨域请求问题

    1.使用jsonp的方式解决跨域请求的问题 我启动两个django项目,然后使用的端口不一样,在项目1中通过ajax发请求给项目2,然后接受项目2发送过来的数据 先看项目1的ajax的代码 $(&qu ...

  10. 博客(二)注册页面django

    首先上一个html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...