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());
}
*/ }

EhCache的配置的更多相关文章

  1. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  2. ehcache.xml配置参数

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  3. hibernate二级缓存ehcache hibernate配置详解

    <!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...

  4. spring中ehcache的配置和使用方法

    继续上篇,这篇介绍服务层缓存,ehcache一般的配置和用法 一.添加jar包引用 修改pom.xml文件,加入: <dependency> <groupId>org.spri ...

  5. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  6. Ehcache缓存配置

    Cache的配置很灵活,官方提供的Cache配置方式有好几种.你可以通过声明配置.在xml中配置.在程序里配置或者调用构造方法时传入不同的参数. 你可以将Cache的配置从代码中剥离出来,也可以在使用 ...

  7. Hibernate学习笔记之EHCache的配置

    Hibernate默认二级缓存是不启动的,启动二级缓存(以EHCache为例)需要以下步骤: 1.添加相关的包: Ehcache.jar和commons-logging.jar,如果hibernate ...

  8. ehcache 的配置

    配置:一.在src目录下加入ehcache.xml: <cache name="SimplePageCachingFilter" maxElementsInMemory=&q ...

  9. 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置

     项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...

随机推荐

  1. jquery属性

    1.toggleClass()  如果对象有class属性,则删除: 如果没有class属性,则加上. <style> .hide{ display: none; } </style ...

  2. css遮罩代码(已验证)

    #mask { background-color: rgb(0, 0, 0); display:none; opacity: 0.0; /* Safari, Opera */ -moz-opacity ...

  3. JVM-String比较-字节码分析

    一道String字符串比较问题引发的字节码分析 public class a { public static void main(String[] args)throws Exception{ } p ...

  4. Java动态代理全面分析

    代理模式 解说:给某一个对象提供一个代理,并由代理对象控制对原对象的引用: 代理模式需要以下几个角色: 1  主题:规定代理类和真实对象共同对外暴露的接口: 2  代理类:专门代理真实对象的类: 3 ...

  5. Windows下安装Nginx+php+mysql环境

    系统:Windows 7 64位系统 安装之前,首先下载软件: Nginx: http://nginx.org/en/download.html PHP Stable PHP 5.6.26: http ...

  6. Java基础学习 -- 接口

    interface是一种特殊的class 接口是纯抽象类 所有的成员函数都是抽象函数: 所有的成员变量都是public static final; 接口是为了方便类的调用 一个类如果要去实现某个接口, ...

  7. 接入统计Crash的工具Crashlytics-ios

    1. 注册账户 登录网站 https://get.fabric.io/ 来注册新的账户 2. account confirmed之后就出现相应的设置页面,设置你的team的名称 3. 出现页面提示下载 ...

  8. css2基础知识梳理

    基础的css知识,只放XMind的截图. css01 css02 css03 css04 css05 css+div布局是前端的基本功,要多多练习.运用标准流.浮动.定位.层级等,做简单的静态页面.一 ...

  9. 转发:Chrome 控制台console的用法

    大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是最喜欢Chrome的,因为它对于调试脚本及前端设计调试都有它比其它浏览器有过之而无不及的地方.可能大家对co ...

  10. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(七)地图打印模块

    config.xml文件的配置如下: <widget label="地图打印" icon="assets/images/map_print.png" co ...