spring整合ehcache的入门级别demo分享,通过junit来测试
最近在做spring和ehcache的整合,遇到不少问题。
问题1
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
版本低的话不支持@CachePut,@CacheEvict,@Cacheable中#result语法,而且不支持接口注解。
问题2
google code上也有一个项目和@Cacheable要实现的功能类似,大家要注意依赖的jar包或maven pom的描述。
问题3
spring中的Cache,CacheManager和net.sf的Cache,CacheManger容易混,大家注意看是哪个包下的。
分享下最终的入门级的demo代码,虽然简单,但把最基本需要的操作都包括了。
运行test包下的com.zuidaima.spring.service.UserService2Test.testCache2输出截图:
>>>>>>>>Start1<<<<<<<<<<<<<<<<<<<<
hits:0
misses:0
>>>>>>>>Finish1<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start save 2<<<<<<<<<<<<<<<<<<<<
hits:0
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish save 2<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findById1 3<<<<<<<<<<<<<<<<<<<<
hits:5
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findById1 3<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findById2 4<<<<<<<<<<<<<<<<<<<<
hits:10
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findById2 4<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findByUsername1 5<<<<<<<<<<<<<<<<<<<<
hits:15
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findByUsername1 5<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findByUsername2 6<<<<<<<<<<<<<<<<<<<<
hits:20
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findByUsername2 6<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findByUsername3 7<<<<<<<<<<<<<<<<<<<<
hits:25
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findByUsername3 7<<<<<<<<<<<<<<<<<<<<
>>>>>>>>Start findByUsername4 8<<<<<<<<<<<<<<<<<<<<
hits:30
misses:0
javaniu@gmail.com->com.zuidaima.spring.entity.User@4e55f7ac
1->com.zuidaima.spring.entity.User@4e55f7ac
javaniu->com.zuidaima.spring.entity.User@4e55f7ac
>>>>>>>>Finish findByUsername4 8<<<<<<<<<<<<<<<<<<<<
可以看到在执行save操作后,后续的按id,name,email查询都是从Cache中获取的。
猜你喜欢
- spring项目整合ehcache和redis缓存实例
- Spring+JMS+ActiveMQ+Tomcat整合项目实例
- spring整合rmi代码实例
- Spring整合DWR实例
- spring整合dwr的入门学习代码实例
- spring MVC+spring+hibernate项目整合入门实例
- spring mvc+spring+mybatis+maven+easyui项目整合代码demo
- Spring整合Quartz实现分布式集群实例
- spring boot+mybatis+ehcache(缓存)整合的简单实例
- maven整合ssm框架+EhCache搭建(eclipse可直接运行)
- spring整合apache activemq实现消息发送的三种方式代码配置实例
- SpringMvc4.x+spring4.x+Mybatis+Maven完整整合项目实例
- /
- /zuidaima_spring-cache-test
- /zuidaima_spring-cache-test/.classpath
- /zuidaima_spring-cache-test/.project
- /zuidaima_spring-cache-test/pom.xml
- /zuidaima_spring-cache-test/src
- /zuidaima_spring-cache-test/src/main
- /zuidaima_spring-cache-test/src/main/java
- /zuidaima_spring-cache-test/src/main/java/com
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/entity
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/service
/zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/service/UserService.java
/zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/service/UserService2.java
/zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/service/UserService2Impl.java
/zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring/service/UserServiceImpl.java
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima/spring
- /zuidaima_spring-cache-test/src/main/java/com/zuidaima
- /zuidaima_spring-cache-test/src/main/java/com
- /zuidaima_spring-cache-test/src/main/resources
- /zuidaima_spring-cache-test/src/main/resources/applicationContext.xml
- /zuidaima_spring-cache-test/src/main/resources/ehcache.xml
- /zuidaima_spring-cache-test/src/main/java
- /zuidaima_spring-cache-test/src/main
- /zuidaima_spring-cache-test