@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其中,注解又叫做"元数据",它为我们在源码中加入信息提供了一种形式化的方法.让我们能在以后的某个时间方便的使用这些数据.更确 ...
随机推荐
- mouseClicked、mousePressed、mouseReleased 的区别
2014年03月16日 21:12:10 xiaobineric 阅读数 9681 标签: 鼠标 事件 关于这3个事件,一直搞不清楚甚至混淆,也已经有一些人说过,但觉得不够明白,最近看了一段教材的 ...
- 【转】利用Behavior Designer制作敌人AI
http://www.unity.5helpyou.com/3112.html 本篇unity3d教程,我们来学习下利用Behavior Designer行为树插件来制作敌人AI,下面开始! Beha ...
- Sum of Squares of the Occurrence Counts解题报告(后缀自动机+LinkCutTree+线段树思想)
题目描述 给定字符串\(S(|S|\le10^5)\),对其每个前缀求出如下的统计量: 对该字符串中的所有子串,统计其出现的次数,求其平方和. Sample Input: aaa Sample Out ...
- POJ3683 Priest John's Busiest Day 【2-sat】
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- input聚焦时,滚动至可视区域
这里的代码来自vux,觉得vux处理得很好,在此记录一下. 当我们在手机上填表单的时候,我们会希望正在填的input或者textarea会自动滚动至可视区域,方便我们边填写边查看内容.以前我的做法是, ...
- Fabric和Sawtooth技术分析(上)
https://mp.weixin.qq.com/s?__biz=MjM5MDAxMTE0MA==&mid=2652049866&idx=1&sn=5b4aea961f3d64 ...
- css的fix高度缺失
这个方法是百度百科手机页面用的,先为固定定位元素设一个父元素,不设高度,不设宽度,什么都不设,他的第一个子元素是我们需要做固定定位的元素,这个按照需求写好样式,此时,父元素的高度依然是0,如何使得父元 ...
- 小米监控 open-falcon部署
具体详情请参考官方文档 http://book.open-falcon.org/zh_0_2/quick_install/ centos6.8 建议centos7系统 否则后面按照官方 ...
- Javaweb测试
1.建立web 工程 输入Project name.然后点击finish. 2.右键点击WebContent-New-JSP File,新建jsp文件. 3.然后编写一个jsp登录的程序 <%@ ...
- git删除远程仓库文件
1 首先将远程代码pull到本地,保持本地仓库跟远端仓库同步 git pull git@github.com:lanleilin/lanGallery.git//使用SSH的方式 2 然后使用git ...