读书人

spring3+ehcache(xml宣言式配置)

发布时间: 2012-07-03 13:37:43 作者: rapoo

spring3+ehcache(xml声明式配置)
从看spring in action 2 中练习的的集成,经过多次实验和网上搜索终于集成好了。

仅仅展示最基本的集成配置,下一篇在整理基于注解式的集成,如果此次无误,注解应该更简单了。

1 springcontext.xml中命名空间配置(部分)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springmodules.org/schema/ehcache
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
">

2 指定保存了自定义的缓存方案的xml文件

<ehcache:config configLocation="classpath:ehcache.xml"/>
3 缓存方案 ehcache.xml(完整)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="500"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"/>
<cache name="demoCache" maxElementsInMemory="500"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"/>
</ehcache>

至于里面的参数的意义大家可以去搜索下把,这里不做说明了

4 在相应的bean上加缓存

假如在此bean上加缓存

<bean id="userDaoImplTarget" ref="dataSource"></property>
</bean>

加缓存的方法为

<ehcache:proxy id="userDaoImpl" refId="userDaoImplTarget">
<ehcache:caching methodName="findAll" cacheName="demoCache"/>
</ehcache:proxy>

说明:id为被代理后的id,refId为要为哪个bean加缓存

然后就是配置方法名(可以使用通配符),使用哪个缓存(来自于缓存配置方案)



5完结:

到此,配置完毕,此时用到的id为userDaoImpl的bean被以代理的方式加上了缓存



副:jar的引入非常重要

除了spring的基本jar包外 关键jar有1、aopalliance-1.0.jar;2、spring-modules-cache-0.8.jar

读书人网 >XML SOAP

热点推荐