EhCache的配置
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的配置的更多相关文章
- (转)springMVC+mybatis+ehcache详细配置
一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...
- ehcache.xml配置参数
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...
- hibernate二级缓存ehcache hibernate配置详解
<!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...
- spring中ehcache的配置和使用方法
继续上篇,这篇介绍服务层缓存,ehcache一般的配置和用法 一.添加jar包引用 修改pom.xml文件,加入: <dependency> <groupId>org.spri ...
- SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
- Ehcache缓存配置
Cache的配置很灵活,官方提供的Cache配置方式有好几种.你可以通过声明配置.在xml中配置.在程序里配置或者调用构造方法时传入不同的参数. 你可以将Cache的配置从代码中剥离出来,也可以在使用 ...
- Hibernate学习笔记之EHCache的配置
Hibernate默认二级缓存是不启动的,启动二级缓存(以EHCache为例)需要以下步骤: 1.添加相关的包: Ehcache.jar和commons-logging.jar,如果hibernate ...
- ehcache 的配置
配置:一.在src目录下加入ehcache.xml: <cache name="SimplePageCachingFilter" maxElementsInMemory=&q ...
- 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置
项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...
随机推荐
- ASP.NET MVC 模块与组件(一)——发送邮件
我的见解: 模块化与组件化是编程的一种思想:提高代码的重用性,提高开发效率. 常见的模块化就是函数与各种类型的封装,若是代码具有更高的重用价值(能够提供给别人使用),建议可以考虑封装成动态链接库(dl ...
- 针对Asp.net MVC SEO的几点建议
1. 引言 SEO 即搜索引擎优化,很多web开发人员本应该熟悉,至少需要了解的一个知识点.像百度.必应等搜索引擎其实一直都在进化.但是有些优化的技巧可能在短时间内不变. 今天就给大家介绍几个专门针对 ...
- SpringMVC的工作原理
1.首先浏览器发送请求给前端控制器DispatcherServlet,DispatcherSerlvet根据请求信息,基于一定的原则选择合适的控制器进行处理并把请求委托给它. 2.页面控制器接收到请求 ...
- ExtJS关于组件Component生命周期
extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有必须的配置设定.事件注册.预渲染处理等都在此时进行. 1.应用组件的配置: 当初始化一个 ...
- 企业管理咨询Interview Checklist
企业管理咨询Interview Checklist 一. 企业战略 1. 您对公司所处行业的看法如何? 2. 请您介绍一下公司的发展历程,主要业务开展状况及核心竞争力.关键成功因素有哪些? 3. 在您 ...
- java web学习总结(十八) -------------------过滤器的高级使用
在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...
- 在网页布局中合理使用inline formating context(IFC)
引子:给大家出一个小小的考题,如何使用css来实现类似下面的在指定区域内,内容自适应的垂直居中.
- iOS 事件处理之UIResponder简介
在用户使用app过程中,会产生各种各样的事件 iOS中的事件可以分为3大类型:触摸事件.加速计事件.远程控制事件 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处 ...
- 用UILocalNotification实现一个闹钟(Swift)
之前项目需求要实现一个闹钟,github上找了半天发现都是很旧的代码了,所以就准备自己写一个,刚好最近在学习Swift,就用Swift写了一个demo放在这里:https://github.com/P ...
- 来自沪江、滴滴、蘑菇街架构师的 Docker 实践分享
架构师小组交流会是由国内知名公司架构师参与的技术交流会,每期选择一个时下最热门的技术话题进行实践经验分享. Docker 作为当前最具颠覆性的开源技术之一,其轻量虚拟化.可移植性是 CI/CD.Dev ...