JPA和Hibernate的二级缓存都是这样做的

代码目录:

这是基础的jar包,如果少的话,再去maven下载

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency> <!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency> <!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->

ehcache.xml :

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<cache name="baseCache"
eternal="false"
maxEntriesLocalHeap=""
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds=""
statistics="true"
timeToLiveSeconds=""/>
</ehcache>

这里采用两种bean的配置方式,一种是xml(EhCacheConfig.xml),一种是java(EhCacheConfig.java),如下:

EhCacheConfig.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 启用缓存 -->
<cache:annotation-driven cache-manager="cacheManager"/> <bean id="cm" class="com.spring.ehcache.CacheMethod"></bean> <bean id="ehCacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean> <!-- 这个bean的id必须叫 cacheManager,不然会报错 No bean named 'cacheManager' is defined-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManagerFactory"/>
<property name="transactionAware" value="true"/>
</bean> <!-- 可以使用ConcurrentMapCacheManager作为缓存管理器,
  它非常简单,对应开发,测试或基础的应用来说,是个不错的选择。但对于生产级别的应用不理想
  用这个替换掉上面的ehCacheManagerFactory和cacheManager这两个bean就行啦

  <bean id="cacheManager"

    class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
  </bean>

-->

<!--

Spring3.1内置了五个缓存管理器实现

SimpleCacheManager

NoOpCacheManager

ConcurrentMapCacheManager

CompositeCacheManager

EhCacheCacheManager

Spring Data又提供了两个缓存器

RedisCacheManager

GemfireCacheManager

-->

</beans>
EhcacheConfig .java:
package com.spring.ehcache;
import net.sf.ehcache.CacheManager; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; @Configuration
@EnableCaching //启用缓存
public class EhcacheConfig { @Bean(name="ehCacheCacheManager")
public EhCacheCacheManager ehCacheCacheManager(CacheManager cm){
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setTransactionAware(true);
ehCacheCacheManager.setCacheManager(cm);
return ehCacheCacheManager;
}
@Bean(name="ehCacheManagerFactoryBean")
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
String src = "ehcache.xml";
System.out.println("EhCacheManagerFactoryBean..");
EhCacheManagerFactoryBean ehFactoryBean =
new EhCacheManagerFactoryBean();
ehFactoryBean.setConfigLocation(new ClassPathResource(src));
return ehFactoryBean;
} @Bean(name="cm")
public CacheMethod cacheMethod(){ return new CacheMethod(); } }

CacheMethod.java: 这个是缓存测试的类,如果有缓存的话,里面的getStr()方法会执行一次,否则会执行多次

package com.spring.ehcache;

import org.springframework.cache.annotation.Cacheable;

public class CacheMethod {
public CacheMethod(){ System.out.println("CacheMethod..");
} //@Cacheable 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就返回缓存的值。否则方法被调用,返回值放入缓存中
//@CachePut 表明Spring应该将方式的缓存值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用
//@CacheEvict 表明Spring应该在缓存中清除一个或多个条目
//@Caching 这是一个分组的注解,能够同时应用多个其他的缓存注解 @Cacheable("baseCache")
public String getStr(){
System.out.println("get Str..");
return "test get str";
} }

TestCache.java

package com.spring.ehcache;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(classes=EhcacheConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
//SpringJUnit4ClassRunner.class使用时要注意,junit的版本要求9以上
public class TestCache {
@Autowired
private CacheMethod cm;
@Autowired
private EhCacheManagerFactoryBean ehCacheManagerFactoryBean;
@Autowired
private EhCacheCacheManager ehCacheCacheManager;
/**
* 使用java配置bean
* */ @Test
public void getCache(){
System.out.println(ehCacheManagerFactoryBean);
System.out.println(ehCacheCacheManager);
System.out.println(cm.getStr());
System.out.println(cm.getStr());
System.out.println(cm.getStr());
} /**
* 使用xml配置bean
*
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("com/spring/ehcache/EhCacheConfig.xml");
System.out.println(app.getBean("ehCacheManagerFactory"));
System.out.println(app.getBean("cacheManager"));
System.out.println(((CacheMethod)app.getBean("cm")).getStr());
System.out.println(((CacheMethod)app.getBean("cm")).getStr());
System.out.println(((CacheMethod)app.getBean("cm")).getStr());
}
*/ }

最新文章

  1. Electron 不完全快速手册
  2. FineReport如何连接和使用MongoDB数据库
  3. Linux下如何不停止服务,清空nohup.out文件
  4. T2 Func&lt;in T1,out T2&gt;(T1 arg)
  5. Jenkins自动部署Tomcat项目
  6. C 语言学习的第 03 课:你的 idea 是怎么变成能够执行的程序的
  7. 【spoj8222】Substrings
  8. SampleDateFormat进行日期格式化
  9. 20145235 《Java程序设计》第10周学习总结
  10. .bak文件在英文版的sqlserver2014如何生成和恢复
  11. java对空格的处理
  12. CentOS6.4下搭建hadoop2.2(64bit)注意事项
  13. Flynn初步:基于Docker的PaaS台
  14. SVG页面loading动态图
  15. 洛谷P3796 【模板】AC自动机(加强版)(AC自动机)
  16. FreeNAS系统总结
  17. JDK动态代理源码解析
  18. Linux服务器配置---安装vsftpd
  19. VS2013下.Net Framework4配置FineUI4.14
  20. [选译]MySQL5.7以上Zip版官方安装文档

热门文章

  1. php中的M方法
  2. CSS3学习总结
  3. oracle 序列、视图、索引
  4. 【JAVA并发编程实战】5、构建高效且可伸缩的结果缓存
  5. 从零开始学 Java - Windows 下安装 JDK
  6. 那些过目不忘的H5页面
  7. iOS UISearchController的使用
  8. ThinkPHP3快速入门教程二:数据CURD
  9. 具备 jQuery 经验的人如何学习AngularJS(附:学习路径)
  10. 下一代Asp.net开发规范OWIN(1)—— OWIN产生的背景以及简单介绍