- 浏览: 834949 次
文章分类
- 全部博客 (365)
- java (124)
- spring mvc (21)
- spring (22)
- struts2 (6)
- jquery (27)
- javascript (24)
- mybatis/ibatis (8)
- hibernate (7)
- compass (11)
- lucene (26)
- flex (0)
- actionscript (0)
- webservice (8)
- rabbitMQ/Socket (15)
- jsp/freemaker (5)
- 数据库 (27)
- 应用服务器 (21)
- Hadoop (1)
- PowerDesigner (3)
- EJB (0)
- JPA (0)
- PHP (2)
- C# (0)
- .NET (0)
- html (2)
- xml (5)
- android (7)
- flume (1)
- zookeeper (0)
- 证书加密 (2)
- maven (1)
- redis (2)
- cas (11)
最新评论
-
zuxianghuang:
通过pom上传报错 Artifact upload faile ...
nexus上传了jar包.通过maven引用当前jar,不能取得jar的依赖 -
流年末年:
百度网盘的挂了吧???
SSO单点登录系列3:cas-server端配置认证方式实践(数据源+自定义java类认证) -
953434367:
UfgovDBUtil 是什么类
Java发HTTP POST请求(内容为xml格式) -
smilease:
帮大忙了,非常感谢
freemaker自动生成源代码 -
syd505:
十分感谢作者无私的分享,仔细阅读后很多地方得以解惑。
Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
spring 3.1中有cache了,下面结合目前网上的一些资料和手册的归纳总结下:
1 @cache注解
在3.1中,都是用注解的了,
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
1 @cache注解
在3.1中,都是用注解的了,
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
- @Cacheable(value = "employee")
- public class EmployeeDAO {
- public Person findEmployee(String firstName, String surname, int age) {
- return new Person(firstName, surname, age);
- }
- public Person findAnotherEmployee(String firstName, String surname, int age) {
- return new Person(firstName, surname, age);
- }
- }
@Cacheable(value = "employee") public class EmployeeDAO { public Person findEmployee(String firstName, String surname, int age) { return new Person(firstName, surname, age); } public Person findAnotherEmployee(String firstName, String surname, int age) { return new Person(firstName, surname, age); } }
在上面的代码中,缓存了Person了,命名为employee,缓存的是方法的值,
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。
在spring 3.1中,可以使用spel表达式去进行缓存的指定,比如:
- @Cacheable(value = "employee", key = "#surname")
- public Person findEmployeeBySurname(String firstName, String surname, int age) {
- return new Person(firstName, surname, age);
- }
@Cacheable(value = "employee", key = "#surname") public Person findEmployeeBySurname(String firstName, String surname, int age) { return new Person(firstName, surname, age); }
这里注意指定的缓存的是根据key=surename。也可以指定表达式
- @Cacheable(value = "employee", condition = "#age < 25")
- public Person findEmployeeByAge(String firstName, String surname, int age) {
- return new Person(firstName, surname, age);
- }
@Cacheable(value = "employee", condition = "#age < 25") public Person findEmployeeByAge(String firstName, String surname, int age) { return new Person(firstName, surname, age); }
这里指定age<25的才缓存;
接下来看下如何应用,比如:
- @Test
- public void testCache() {
- Person employee1 = instance.findEmployee("John", "Smith", 22);
- Person employee2 = instance.findEmployee("John", "Smith", 22);
- assertEquals(employee1, employee2);
- }
@Test public void testCache() { Person employee1 = instance.findEmployee("John", "Smith", 22); Person employee2 = instance.findEmployee("John", "Smith", 22); assertEquals(employee1, employee2); }
这个时候肯定是相等的了,因为用的是缓存。但是如果调用的是
findEmployeeBySurname方法的话,就一定有点不同了,
- @Test
- public void testCacheOnSurnameAsKey() {
- Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22);
- Person employee2 = instance.findEmployeeBySurname("Jack", "Smith", 55);
- assertEquals(employee1, employee2);
- }
@Test public void testCacheOnSurnameAsKey() { Person employee1 = instance.findEmployeeBySurname("John", "Smith", 22); Person employee2 = instance.findEmployeeBySurname("Jack", "Smith", 55); assertEquals(employee1, employee2); }
但由于是缓存的是根据surename为key,所以上面结果两个对象却依然是相等的(尽管原本看上去是不同的对象了),所以key的选择一定要小心。
继续单元测试:
- @Test
- public void testCacheWithAgeAsCondition() {
- Person employee1 = instance.findEmployeeByAge("John", "Smith", 22);
- Person employee2 = instance.findEmployeeByAge("John", "Smith", 22);
- assertEquals(employee1, employee2);
- }
@Test public void testCacheWithAgeAsCondition() { Person employee1 = instance.findEmployeeByAge("John", "Smith", 22); Person employee2 = instance.findEmployeeByAge("John", "Smith", 22); assertEquals(employee1, employee2); }
这两个就一样了,因为都是age<25的,都缓存了,指向同一个对象。
2 取消缓存
下面看下如何取消缓存
@CacheEvict
@CacheEvict(value = "employee", allEntries = true) public void resetAllEntries() { }
使用@CacheEvict去取消缓存,
@CacheEvict支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
当然,也可以@cahceable和@cacheEvict一起使用,比如:
- @CacheEvict(value = "employee", beforeInvocation = true)
- @Cacheable(value = "employee")
- public Person evictAndFindEmployee(String firstName, String surname, int age) {
- return new Person(firstName, surname, age);
- }
@CacheEvict(value = "employee", beforeInvocation = true) @Cacheable(value = "employee") public Person evictAndFindEmployee(String firstName, String surname, int age) { return new Person(firstName, surname, age); }
- @Test
- public void testBeforeInvocation() {
- Person employee1 = instance.evictAndFindEmployee("John", "Smith", 22);
- Person employee2 = instance.evictAndFindEmployee("John", "Smith", 22);
- assertNotSame(employee1, employee2);
- }
@Test public void testBeforeInvocation() { Person employee1 = instance.evictAndFindEmployee("John", "Smith", 22); Person employee2 = instance.evictAndFindEmployee("John", "Smith", 22); assertNotSame(employee1, employee2); }
这里的话,先使用@CacheEvict(value = "employee", beforeInvocation = true),
会先清掉所有缓存,所以asset的结果就不相等了;
3 如何配置
.spring-cache
首先我们来看一下如何使用spring3.1自己的cache,
需要在命名空间中增加cache的配置
- beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
- <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
- <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="default" />
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="andCache" />
- </set> </property>
- </bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager"/> <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="default" /> <bean class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean" p:name="andCache" /> </set> </property> </bean>
spring对ehcache并没有很好的支持,不建议使用,可以参看http://hanqunfeng.iteye.com/blog/1204343
发表评论
-
jackson annotations注解详解
2015-01-13 11:34 18900官方WIKI:https://github.com/Fast ... -
Ehcache 整合Spring 使用页面、对象缓存
2012-11-02 19:56 1021Ehcache在很多项目中都出现过,用法也比较简 ... -
Spring 3.1 M1 中的缓存功能
2012-11-02 19:26 796本文转自:http://www.oschina.net/ ... -
深入剖析Spring Web源码(八) - 处理器映射,处理器适配器以及处理器的实现 - 基于简单控制器流程的实现
2012-10-31 13:37 12101.1.1.1 ... -
spring 源码 阅读 笔记 之 HandlerMapping
2012-10-31 12:59 1608SpringCVSBeanHTMLAnt ... -
spring mvc重复提交拦截器方法
2012-10-31 11:37 8960import javax.servlet.http.HttpS ... -
不重复配置——利用Spring通用化配置
2012-10-17 09:40 945还记得 如下这种配置吗: 1、struts2作用域 ... -
Ehcache 整合Spring 使用页面、对象缓存
2012-10-16 09:44 751Ehcache在很多项目中都出现过,用法也比较简单。一般的 ... -
SpringMVC文件上传 多文件
2012-10-15 17:27 7034必须明确告诉DispatcherServlet如何处理Mult ... -
Spring MVC 3.1新特性 生产者、消费者请求限定
2012-10-12 11:50 11026.6.5、生产者、消费者限定 6.6.5.1、基本概念 ... -
spring + mybatis 多数据源切换
2012-10-10 11:42 1522[代码] DbContextHold ... -
spring MVC 文件上传
2012-10-06 10:03 2020spring支持在网络应用程序处理文件上传,提供拔插的org. ... -
JasperReport与spring集成的三种方式
2012-09-26 17:29 1780最近要用JasperReport,试着和sprin ... -
Spring MVC+Jasper Report 及生成PDF的中文问题
2012-09-26 17:10 2先说两句报表框架的选择,JasperRepor ... -
Springmvc与jasperreport结合生成报表的一种方法
2012-09-26 16:42 1<script type="text/ja ... -
Spring MVC和Struts2的区别
2012-09-08 08:59 10021. 机制:spring mvc的入口是servlet,而st ... -
SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结
2012-06-19 17:34 1344一 开发环境 1、动态web工程 2、部分依赖 ... -
用spring MVC 生成Excel和PDF
2012-06-16 19:16 35891 用spring MVC 生成Excel和PDF http ... -
SpringMVC+FreeMarker实现半自动静态化
2012-06-14 13:53 2465感谢imyourgod的原贴http://to ... -
文件下载(只需要简单的四步),Java中都通用
2012-06-15 08:33 1190Javadownload文件下载Spring ...
相关推荐
Spring 3.1 jar包Spring 3.1 jar包Spring 3.1 jar包Spring 3.1 jar包Spring 3.1 jar包
Spring 3.1的 源码,在IDE中关联即可查看框架源码
spring3.1完整包
spring3.1最新版本的开发宝典,内容详解!
Spring3.1AOP简单例子,需要的jar包分开上传了
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
Spring 3.1倚赖的外部jar包汇总
spring3.1需要的jar包,自身实验过,调试过程序可以正常运行
此包为本人亲测spring3.1所需的全部jar包
spring3.1+xfire1.26 全注解
Spring3.1 中文参考文档,可以参考下
最新的spring3.1 jar包都在里面,很齐全了!!!
spring 中文开发文档 spring 3.1
spring3.1 struts2.3 hibernate4.1 jpa集成小例子,实现查询和添加小功能,删除和修改同理自己写吧。
spring3.1源码 !
spring3.1+hibernate4.1+struts2整合所有jar包
Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架
spring 开发工具全套 包含core、aop、beans等等 一应俱全
Spring3.1 MongoDB整合实例(含jar包)已测