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"
[b]xmlns:ehcache="http://www.springmodules.org/schema/ehcache"[/b] xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
[b]http://www.springmodules.org/schema/ehcache
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd[/b]">

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

[b]<ehcache:config configLocation="classpath:ehcache.xml"/>[/b]
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" class="org.fwb.dao.impl.UserDaoImpl">
<property name="ds" 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有[b]1、aopalliance-1.0.jar;2、spring-modules-cache-0.8.jar[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值