---
title: "ehcache缓存方法返回结果（二）--annotation"
author: "lastsoul"
date: "2015-11-26T14:17:05Z"
tags: []
categories: ["编程语言"]
slug: "ehcache-annotation"
draft: false
lastmod: "2015-11-26T14:22:26Z"
id: 2259446
url: "https://lastsoul.iteye.com/blog/2259446"
---

缓存一个方法的返回值，除了上篇文章的AOP方式，也可以通过手动的在某个方法上面加一个annotation的方法来实现，比AOP更加灵活，比如有些方法命名没有规律，不好AOP。缺点是侵入了方法内部，要在方法上面写annotation。 实现方法是在基于上篇文章\[url] http://lastsoul.iteye.com/admin/blogs/2246566\[/url]的基础上，在spring-ehcache.xml上加上： \[code="java"] \[/code] java代码： \[code="java"]@Cacheable(value="SimplePageCachingFilter") @Override public User findById(Integer id) { System.out.println(id); User user=new User(); user.setId(System.currentTimeMillis()+""); user.setName("test"); return user; }\[/code] 其中SimplePageCachingFilter是ehcache.xml中的cachename.