- 浏览: 1722278 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (337)
- javaScript校验 (11)
- java (31)
- java连接数据库 (1)
- js应用 (41)
- JQuery (15)
- linux操作命令 (12)
- loadrunner测试 (5)
- tomcat (18)
- 数据库 (12)
- eclipse (7)
- 触发器 (7)
- 表单 (3)
- ibatis (6)
- oracle (4)
- xml (1)
- Exception (6)
- spring (16)
- struts (4)
- struts 标签 (2)
- sql (8)
- sql server (6)
- 其它 (18)
- Apache (2)
- 电脑故障 (4)
- java 线程 (1)
- dwr (8)
- jackey (18)
- 总结 (34)
- gcc linux (2)
- extjs 学习 (5)
- 网站建设 (4)
- 健康 (2)
- 房地产知识 (1)
- hibernate (3)
- mysql (7)
- linux (13)
- svn (1)
最新评论
-
阳光泛滥的日子:
很好用谢谢
java.net.URISyntaxException的解决办法 -
linjianqing:
现在有更好的处理方式吗?我正也为这发愁
applet访问打印机出现的问题 -
ruyi574812039:
非常感谢您的总结!想问几个问题!假设三个项目分别以静态部署的三 ...
在Tomcat中部署Web程序的几种方式 -
yangguo:
太j8麻烦了
Spring3.1中使用缓存注解及Shiro的缓存联合 -
bellawang:
很好用谢谢
java.net.URISyntaxException的解决办法
spring 3.1中的cache小结
- 博客分类:
- spring
spring 3.1中有cache了,下面结合目前网上的一些资料和手册的归纳总结下:
1 @cache注解
在3.1中,都是用注解的了,
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
在上面的代码中,缓存了Person了,命名为employee,缓存的是方法的值,
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。
在spring 3.1中,可以使用spel表达式去进行缓存的指定,比如:
这里注意指定的缓存的是根据key=surename。也可以指定表达式
这里指定age<25的才缓存;
接下来看下如何应用,比如:
这个时候肯定是相等的了,因为用的是缓存。但是如果调用的是
findEmployeeBySurname方法的话,就一定有点不同了,
但由于是缓存的是根据surename为key,所以上面结果两个对象却依然是相等的(尽管原本看上去是不同的对象了),所以key的选择一定要小心。
继续单元测试:
这两个就一样了,因为都是age<25的,都缓存了,指向同一个对象。
2 取消缓存
下面看下如何取消缓存
@CacheEvict
使用@CacheEvict去取消缓存,
@CacheEvict支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
当然,也可以@cahceable和@cacheEvict一起使用,比如:
这里的话,先使用@CacheEvict(value = "employee", beforeInvocation = true),
会先清掉所有缓存,所以asset的结果就不相等了;
3 如何配置
.spring-cache
首先我们来看一下如何使用spring3.1自己的cache,
需要在命名空间中增加cache的配置
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>
评论
1 楼
520191853
2012-11-23
你试着在EmployeeDAO类上随便实现个接口。
public interface IService {
public String test();
}
public class EmployeeDAO implements IService{
public String test() {
return null;
}
}
好像就不能用了
public interface IService {
public String test();
}
public class EmployeeDAO implements IService{
public String test() {
return null;
}
}
好像就不能用了
发表评论
-
ehcache 缓存监听器的使用CacheEventListener
2012-09-24 14:33 7618ehcache通过配置,可以实现诸如,内容最长缓存时间,内容最 ... -
Spring3.1中使用缓存注解及Shiro的缓存联合
2012-09-24 12:00 15399Spring最近释出了3.1的REALEASE版本,到我写这篇 ... -
spring Failed to convert property value of type [$Proxy10] to required type
2012-09-12 21:42 4781org.springframework.beans.facto ... -
关于Spring事务代理类型转换问题($ProxyXX cannot be cast to 类型)(
2012-09-12 21:38 4232二、解决方案 后来在网上搜了一下,这样的问题很多 ... -
Spring MVC:使用SimpleUrlHandlerMapping的一个简单例子
2009-09-27 00:50 4993实现一个控制器ShirdrnController,如下所示: ... -
ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的路径设置祥解
2009-09-19 10:24 3638package com.openv.spring; ... -
ClassPathXmlApplicationContext 和FileSystemXmlAppli
2009-06-24 12:45 1374ClassPathXmlApplicationContext ... -
PropertyPlaceholderConfigurer
2008-12-03 10:38 2566Spring的框架中为您提供了一个 BeanFactoryPo ... -
Spring MVC 入门 实例
2008-07-03 10:55 5680下面开始开发Spring MVC应用程序 (2)修改WEB- ... -
Spring MVC 配置
2008-07-03 09:38 2060一,配置分发器 DispatcherServlet 是Spri ... -
JdbcTemplate - 查询
2008-06-20 10:40 6789使用JdbcTemplate进行查询时,可以使用queryF ... -
转载]spring+hibernate架构中Dao访问数据库的几种方法
2008-06-20 10:28 7684spring+hibernate架构中Dao访问数据库的几种方 ... -
JdbcTemplate总结
2008-06-20 10:10 26461.您可以使用JdbcTemplate的execute()方法 ... -
//插入很多书(批量插入用法)jdbcTemplate
2008-06-20 10:09 6863//插入很多书(批量插入用法) public void in ... -
JdbcTemplate学习笔记
2008-06-20 09:16 19301、使用JdbcTemplate的execute()方法执行S ...
相关推荐
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包)已测