@Cacheable注解在spring3中的使用-实现缓存
转: http://blog.csdn.net/chenleixing/article/details/44815443
在软件开发中使用缓存已经有一个非常久的历史了。缓存是一种很好的设计思想,一旦你用了他,你将会发现他确实很有用。Spring3.1版本的核心对缓存做了实现。在Java推出Annotation特性之前,实现缓存的一个难点在于它与业务逻辑代码的耦合性太强。
然而,Spring3.1中使用@Cacheable 和@CacheEvict实现缓存在某种程度上解决了这个问题,基本思想是在方法加上@Cacheable注解,这个方法的返回值将具有缓存特性。
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Cacheable(value = "employee")publicclassEmployeeDAO { publicPerson findEmployee(String firstName, String surname, intage) { returnnewPerson(firstName, surname, age); } publicPerson findAnotherEmployee(String firstName, String surname, intage) { returnnewPerson(firstName, surname, age); }} |
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。
|
1
2
3
4
5
|
@Cacheable(value = "employee") publicPerson findEmployee(String firstName, String surname, intage) { returnnewPerson(firstName, surname, age); } |
任何存储在缓存中的数据为了高速访问都需要一个key。spring默认使用被@Cacheable注解的方法的签名来作为key,当然你可以重写key,自定义key可以使用SpEL表达式。
|
1
2
3
4
|
<span style="font-size:14px;">@Cacheable(value = "employee", key = "#surname")</span> publicPerson findEmployeeBySurname(String firstName, String surname, intage) { returnnewPerson(firstName, surname, age); } |
在findEmployeeBySurname()的注解中"#surname"是一个SpEL表达式,他将使用findEmployeeBySurname()方法中的surname参数作为key。
|
1
2
3
4
5
|
@Cacheable(value = "employee", condition = "#age < 25") publicPerson findEmployeeByAge(String firstName, String surname, intage) { returnnewPerson(firstName, surname, age); } |
|
1
2
3
4
5
6
7
8
|
@Test publicvoidtestCache() { Person employee1 = instance.findEmployee("John", "Smith", 33); Person employee2 = instance.findEmployee("John", "Smith", 33); assertEquals(employee1, employee2); } |
|
1
2
3
4
5
6
7
8
|
@Test publicvoidtestCacheWithAgeAsCondition() { Person employee1 = instance.findEmployeeByAge("John", "Smith", 33); Person employee2 = instance.findEmployeeByAge("John", "Smith", 33); assertEquals(employee1, employee2); } |
|
1
2
3
4
5
6
7
8
|
@Test publicvoidtestCacheOnSurnameAsKey() { Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22); Person employee2 = instance.findEmployeeBySurname("Jack", "Smith", 55); assertEquals(employee1, employee2); } |
@Cacheable注解在spring3中的使用-实现缓存的更多相关文章
- Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效)
Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效) 前言 今天在使用Spring cache的Cacheable注解的过程中遇见了一个Cacheable注解失效的问题, ...
- 在Spring3中使用注解(@Scheduled)创建计划任务
Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了: 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下: 在S ...
- Spring @Cacheable注解 && 事务@Transactional 在同一个类中的方法调用不生效
@Cacheable 注解在对象内部调用不会生效 代码示例:ProductServiceImpl.java public List<ProductInfoVO> getProductLis ...
- spring redis @Cacheable注解使用部分错误及无效原因
spring redis @Cacheable注解使用部分错误及无效原因 说明: spring项目用到redis注解无效,解决问题中遇到一堆BUG,各种搜索,看了许多错误解决方案一一测试,对于 ...
- 小白的springboot之路(八)、继承Redis以及@Cacheable注解实现Redis缓存
0.前言 在项目中,缓存作为一种高效的提升性能的手段,几乎必不可少,Redis作为其中的佼佼者被广泛应用: 一.spring boot集成Redis 1.添加依赖 <dependency> ...
- @Cacheable注解不生效原因
因为@Cacheable注解应用了AOP动态代理,生成代理类,判断缓存中是否存在该key,如果不存在则调用被代理类的标有@Cachable注解的方法,否则不执行. 所以当类A的方法a调用方法b(标有@ ...
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...
- Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法
Spring MVC 中采用注解方式 Action中跳转到另一个Action的写法 在Action中方法的返回值都是字符串行,一般情况是返回某个JSP,如: return "xx" ...
- 注解在android中的使用
注解在android程序中的使用 何为注解: 在Java其中,注解又叫做"元数据",它为我们在源码中加入信息提供了一种形式化的方法.让我们能在以后的某个时间方便的使用这些数据.更确 ...
随机推荐
- MySql数据库 - 4.可视化操作数据库
创建表 对表中数据进行 增.删.改.查 查 右键刚刚创建的表 - 选择查看前 1000 条数据 增.改 表格必须有主键才能添加数据,主键是不能重复的 1. 右键表 - 查看前 1000 条数据 2. ...
- SOCK5代理服务器
SOCK5代理服务器 简单介绍下比较好用的代理服务器,在部署过程中在内外网访问,需要切网比较麻烦,所以可以在互联网区部署sock5代理,通过配置代理切换服务器, 比较方便配置,节省时间. 官网: ht ...
- C++字符串高效查找替换,有空分析分析
void CWebTransfer::Substitute(char *pInput, char *pOutput, char *pSrc, char *pDst) { char *pi, *po, ...
- Kernel Space与User Space(转)
对于刚刚接触Linux的菜鸟来说,可能会不理解大家常说的Kernel Space和User Space是什么意思,我简单搜了一下,发现阮一峰写过一个比较简洁的介绍,贴下来给大家: 学习 Linux 时 ...
- pc端自适应方案
一.常见处理方式 定宽 电商类.内容为主的网站几乎采用这种方式 1.网易考拉.京东(1190px) 2.知乎(1000px),果壳(1000px),网易新闻(1200px) 媒体查询+定宽 图片类.简 ...
- spring boot 排除个别配置类的代码
废话不说,直接上代码 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfigu ...
- POJ1385 Lifting the Stone
There are many secret openings in the floor which are covered by a big heavy stone. When the stone i ...
- 洛谷 P1783 海滩防御
题目描述 WLP同学最近迷上了一款网络联机对战游戏(终于知道为毛JOHNKRAM每天刷洛谷效率那么低了),但是他却为了这个游戏很苦恼,因为他在海边的造船厂和仓库总是被敌方派人偷袭.于是,WLP动用了他 ...
- javascript屏蔽脏字
原文发布时间为:2009-04-16 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...
- WSDL协议简单介绍
WSDL – WebService Description Language – Web服务描述语言 通过XML形式说明服务在什么地方-地址. 通过XML形式说明服务提供什么样的方法 – 如何调用. ...