8 -- 深入使用Spring -- 5...1 启用Spring缓存
8.5.1 启用Spring缓存
Spring配置文件专门为缓存提供了一个cache:命名空间,为了启用Spring缓存,需要在配置文件中导入cache:命名空间。
导入cache:命名空间之后,启用Spring缓存还要两步:
① 在Spring配置文件中添加<cache:annotation-driven cache-manager="缓存管理器ID"/>,该元素指定Spring根据注解来启用Bean级别或方法级别的缓存。
② 针对不同的缓存实现配置对应的缓存管理器。
第1步使用<cache:annotation-driven.../>元素时可通过cache-manager显式指定容器中缓存管理器的ID;该属性的默认值为cacheManager ------ 也就是说,如果将容器中缓存管理器的ID设为cacheManager,则可省略<cache:annotation-driven.../>的cache-manager属性。
第2步由于Spring底层可使用大部分主流的Java缓存工具,而不同的缓存工具所需的配置也不同,因此略微有点麻烦。
以Spring内置的缓存实现和EhCache为例来介绍Spring缓存的配置:
1.Spring 内置缓存实现的配置
Spring内置的缓存实现只是一种内存中的缓存,并非真正的缓存实现,因此通常只能用于简单的测试环境,不建议在实际项目中使用Spring内置的缓存实现。
Spring内置的缓存实现使用SimpleCacheManager作为缓存管理器,使用SimpleCacheManager配置缓存非常简单,直接在Spring容器中配置该Bean,然后通过<property.../>驱动该缓存管理器执行setCaches()方法来设置缓存区即可。
SimpleCacheManager是一种内存中的缓存区,底层直接使用了JDK的ConcurrentMap来实现缓存,SimpleCacheManager使用了ConcurrentMapCacheFactoryBean作为缓存区,每个ConcurrentMapCacheFactoryBean配置一个缓存区。
配置Spring内置缓存的缓存管理器DEMO:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 使用SimpleCacheManager配置Spring内置的缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<!-- 配置缓存区 -->
<property name="caches">
<set>
<!-- 使用ConcurrentMapCacheFactoryBean配置缓存区 -->
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!-- 为缓存区指定名字 -->
<property name="name" value="default"/>
</bean>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!-- 为缓存区指定名字 -->
<property name="name" value="users"/>
</bean>
</set>
</property>
</bean> </beans>
配置文件使用SimpleCacheManager配置了Spring内置的缓存管理器,并为该缓存管理器配置了两个缓存区:default和users ------ 这些缓存区的名字很重要,因为后面使用注解驱动缓存时需要根据缓存区的名字来将缓存放入指定缓存区内。
提示:
在实际的应用中,开发者可以根据自己的需要,配置更多的缓存区,一般来说,应用有多少个组件需要缓存,程序就应该配置多少个缓存区。
2.EhCache缓存实现的配置
在配置EhCache缓存实现之前,首先需要将EhCache缓存的JAR包添加到项目的类加载路径中。
ehcache-core-x.x.x.jar : 是EhCache的核心JAR包;
slf4j-api-x.x.x.jar : 是该缓存工具所使用的日志工具;
为了使用EhCache,同样需要在应用的类加载路径下添加一个ehcache.xml配置文件。
ehcache.xml :
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<!-- 配置默认的缓存区 -->
<defaultCache maxElementsInMemory="10000" eternaml="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<!-- 配置名为users的缓存区 -->
<cache name="users" maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" />
</ehcache>
其中第一个用于配置匿名的、默认的缓存区,第二个才是配置了名为users的缓存区。如果需要,完全可以将<cache.../>元素复制多个,用于配置多个有名字的缓存区。这些缓存区的名字同样很重要,后面使用注解驱动缓存时需要根据缓存区的名字来将缓存数据放入指定缓存区内。
Spring使用EhCacheCacheManager作为EhCache缓存实现的缓存管理器,因此只要该对象配置在Spring容器中,它就可作为缓存管理器使用,但EhCacheCacheManager底层需要依赖一个net.sf.ehcache.CacheManager作为实际的缓存管理器。
为了将net.sf.ehcache.CacheManager纳入Spring容器的管理之下,Spring提供了EhCacheManagerFactoryBean工厂Bean,该工厂Bean实现了FactoryBean<CacheManager>接口。当程序把EhCacheManagerFactoryBean部署在Spring容器中,并通过Spring容器请求获取工厂Bean时,实际返回的是它的产品 ------ 也就是CacheManager对象。因此,为了在Spring配置文件中配置基于EhCache的缓存管理器,只要增加如下两段配置即可:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置EhCache的CacheManager通过configLocation指定ehcache.xml文件的位置 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="false"/>
</bean>
<!-- 配置基于EhCache的缓存管理器 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehCacheManager"/>
</bean> </beans>
Spring配置文件中,第一个Bean是一个工厂Bean,它用于配置EhCache的CacheManager;第二个Bean才是为Spring缓存配置的基于EhCache的缓存管理器,该缓存管理器需要依赖于CacheManager,因此程序将第一个Bean注入到第二个Bean中
配置好人一种缓存管理器之后,接下来就可使用注解来驱动Spring将缓存数据存入指定缓存区了。
8 -- 深入使用Spring -- 5...1 启用Spring缓存的更多相关文章
- 【spring boot】15.spring boot项目 采用Druid数据库连接池,并启用druid监控功能
在http://www.cnblogs.com/sxdcgaq8080/p/9039442.html的基础上,来看看spring boot项目中采用Druid连接池. GitHub地址:示例代码 == ...
- Spring 整合 Hibernate 时启用二级缓存实例详解
写在前面: 1. 本例使用 Hibernate3 + Spring3: 2. 本例的查询使用了 HibernateTemplate: 1. 导入 ehcache-x.x.x.jar 包: 2. 在 a ...
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
- spring ehcache 页面、对象缓存
一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...
- Ehcache 整合Spring 使用页面、对象缓存
Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...
- (转)Ehcache 整合Spring 使用页面、对象缓存
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- Ehcache 整合Spring 使用页面、对象缓存(转载)
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
- 使用 Spring Boot 快速构建 Spring 框架应用,PropertyPlaceholderConfigurer
Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.S ...
- Ehcache 整合Spring 使用页面、对象缓存(转)
Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...
随机推荐
- 喵哈哈村的魔法考试 Round #9 (Div.2) 题解
A题 喵哈哈村的数据筛选游戏 题解:这道题签到题,拿个数组记录一下这个数是否出现过即可. #include<bits/stdc++.h> using namespace std; cons ...
- Python 八大排序算法速度比较
这篇文章并不是介绍排序算法原理的,纯粹是想比较一下各种排序算法在真实场景下的运行速度. 算法由 Python 实现,用到了一些语法糖,可能会和其他语言有些区别,仅当参考就好. 测试的数据是自动生成的, ...
- WinPython
WinPython http://winpython.github.io/
- TensorFlow进阶(二)---张量的操作
张量操作 在tensorflow中,有很多操作张量的函数,有生成张量.创建随机张量.张量类型与形状变换和张量的切片与运算 生成张量 固定值张量 tf.zeros(shape, dtype=tf.flo ...
- [Python设计模式] 第6章 衣服搭配系统——装饰模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格( ...
- 动态规划之 <筷子>
描述 A 先生有很多双筷子.确切的说应该是很多根,因为筷子的长度不一,很难判断出哪两根是一双的.这天,A 先生家里来了K 个客人,A 先生留下他们吃晚饭.加上A 先生,A夫人和他们的孩子小A,共K+3 ...
- 熬之滴水成石:最想深入了解的内容--windows内核机制(6)
58 进程和线程(3) 说完进程再说说线程,线程相比于进程其实有更多可说的内容.首先实现线程调用的数据结构是个栈,该栈记录了调用方法的信息这里面也包括了函数调用及返回的地址.线程肯定是属于某个进程,其 ...
- JAVA分库分表的实现方案
分库分表的实现方案无非2种:1.本地,2.远程.而在本地一般有2种实现(1.业务代码级别 2.jdbc级别), 其中jdbc级别的本地代理方案的代表有:当当开源的 shardingsphere,远 ...
- GDALSetProjection使用的一个注意事项
GDALSetProjection 简述 GDALSetProjection是用来给GDALDataset设定投影信息(坐标系统)的接口,实际上是GDALDataset::SetProjection这 ...
- linux达人养成计划学习笔记(七)—— 用户登录查看命令
一.查看用户登录信息 1.命令格式 w 2.命令结果 第一行信息是:系统当前时间 系统运行总时间 登录用户数量 一分钟/五分钟/十分钟的系统负载(越大越差) 二.who命令 1 ...