spring基于注解Ehcache缓存整合
2013-08-05 00:19
155 查看
今天下午做了一下缓存整合在项目中,以前没有用过也没有学过。都知道spring已经对Ehcache进行了很好的支持,我的spring版本是3.2.2在spring-context-support.jar包中可以看到.以前的版本可能会不太一样。
我下的ehcache是ehcache-web-2.0.4-distribution.gz和ehcache-2.7.3-distribution.tar.gz一个做页面的缓存一个做查询的缓存。
echcache的配置也不说了网上一大堆,而且解释的也很清楚。但是spring基于注解的整合好像还不太一样,好吧看官方文档进行配置好一点。
官网上有这么一句话:EHCache support moved to spring-context-support 看来以前的版本还不是在上面提到的那个包中。接着看配置吧:
OK!!缓存已经完成了
说明一下官网上没有p:shared="true"在配echcahe的时候,这时启动会报一个错误:
我找了半天也不知道起动的时候哪还创建了一个CancheManager但加上
p:shared="true"就没事了。命名空间的p打不开我也不知道是什么意思。请高手指点。我觉得大概意思就是使用一个cache吧。
做一个简单的测试!
test类
OK。很明显可以看到两个对象是一模一样的。好吧配置成功能了可以在其他需要的地方方便使用。还有一个注解是@CacheEvict是删除操作。页面缓存明天再写,也有好多不明白的地方。@晓骏 看完记的指点一下哈!
我下的ehcache是ehcache-web-2.0.4-distribution.gz和ehcache-2.7.3-distribution.tar.gz一个做页面的缓存一个做查询的缓存。
echcache的配置也不说了网上一大堆,而且解释的也很清楚。但是spring基于注解的整合好像还不太一样,好吧看官方文档进行配置好一点。
官网上有这么一句话:EHCache support moved to spring-context-support 看来以前的版本还不是在上面提到的那个包中。接着看配置吧:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <cache:annotation-driven /> </beans>这是官方文档中的基于注解的XML配置如果加上缓存的配置的话上面的配置还要加上下面这一句:
xmlns:p="http://www.springframework.org/schema/p"缓存的配置如下:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/> <!-- EhCache library setup --> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true"/>
OK!!缓存已经完成了
说明一下官网上没有p:shared="true"在配echcahe的时候,这时启动会报一个错误:
net.sf.ehcache.CacheException:Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following
我找了半天也不知道起动的时候哪还创建了一个CancheManager但加上
p:shared="true"就没事了。命名空间的p打不开我也不知道是什么意思。请高手指点。我觉得大概意思就是使用一个cache吧。
做一个简单的测试!
@Cacheable(value="sampleCache1",key="#id") public T getById(String id) { return (T) getSession().get(clazz, id); }在baseDao的get方法上进行配置在测试类中用userService服务进行测试看输出几条sql语句:
test类
public class CancheSpring extends BaseSpringTest { @Resource private UserService userService; @Test public void testGetCanche(){ String id = "40288183401e060a01401e06116b0000"; User user = userService.getById(id); System.out.println(user); User user2 = userService.getById(id); System.out.println(user2); } }当然id我从数据库中直接copy出来的UUID.输出结果如下:
OK。很明显可以看到两个对象是一模一样的。好吧配置成功能了可以在其他需要的地方方便使用。还有一个注解是@CacheEvict是删除操作。页面缓存明天再写,也有好多不明白的地方。@晓骏 看完记的指点一下哈!
相关文章推荐
- 基于spring注解,泛型的使用
- Spring - 基于Java配置的注解
- spring中事务的管理基于xml和注解,以及spring在web中应用
- Spring 声明式事务管理----基于注解方式
- spring-基于注解(Annotation)的配置(转)
- Spring基于java注解配置
- 【第十二章】零配置 之 12.3 注解实现Bean定义、12.4基于Java类定义Bean配置元数据 ——跟我学spring3
- java web之路 spring 基于@Autowried注解的依赖注入
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- spring 基于注解的@Scheduled和quartz定时器两种实现
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- spring注解基于Annotation的依赖注入配置笔记
- spring自带的定时任务功能,基于注解和xml配置(一)
- 基于注解的mybatis和spring整合
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。
- Spring Boot之基于注解的数据格式化
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- Spring4 学习系列之——整合基于注解的Hibernate实例
- service层spring事务配置(基于注解)