posts - 156,  comments - 601,  trackbacks - 0

前言:
  本文档将讲解一下,如何借助Spring Module项目,实现配置声明性缓存功能。
 
   声明式缓存配置有效地将缓存功能从应用程序的核心需求中分离出来,解决了上文提及的众多问题,还带来了以下优点:
  • 更明晰的责任分离。系统中的模块仅负责其核心需求,不再负责缓存功能。这将带来更出色的可追溯性。
  • 更高的模块化程度。将缓存功能从核心模块中分离出来减少了重复代码(支持“一次且仅一次”原则),并有助于避免代码混乱。
  • 设计决策的后期绑定。使用声明式缓存,开发人员可将与缓存实现和调优有关的决策制订延后。开发人员可将精力集中在应用程序的当前核心需求上。声明式配置支持 YAGNI(“you aren't gonna need it”的缩写,意为“您将不会需要它)原则,仅在开发人员确实需要的时候才允许他们为应用程序添加缓存,而且不需要进行系统级的更改。

说明:
  本档的配置经过本人测试,都能正确运行。
  运行环境: Jdk5.0, Spring-2.5, Spring-modules-0.9, ehcache-1.6.0-beta4.jar

首先创建一个StudentService服务类,本文将对其所有的以get* 方式命令的方法,进行缓存处理。当调用set* 命令时,需要其删除缓存
以更做数据的更新。 
源代码如下:
 1 public class StudentService {
 2 
 3     private String name = "matthew";
 4     
 5     public String getName() {
 6         return name;
 7     }
 8     
 9     public String getName(String salution) {
10         return salution + " " + name;
11     }
12     
13     public void setName(String name) {
14         this.name = name;
15     }
16     
17     public void changeNameAndNotTellCache(String name) {
18         this.name = name;
19     }
20 }


接下来,就是编写Spring配置文件 context.xml,以实现根据上面的要求,进行声明性缓存功能的配置

Spring的配置内容如下:(已加上注释)
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 5 
 6     <!-- Using a EHCache cache manager -->
 7     <bean id="cacheManager"
 8         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
 9         <!--<property name="cacheManagerName" value="mainCache"/>-->
10         <property name="configLocation" value="classpath:ehcache.xml" />
11     </bean>
12 
13     <!-- 使用Spring Modules对 EhCache的封装  -->
14     <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
15         <property name="cacheManager" ref="cacheManager" />
16     </bean>
17     
18     <!-- 配置 方法 拦截器  -->
19     <!-- 缓存拦截器 -->
20     <bean id="cachingInterceptor"
21         class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">
22         <property name="cacheProviderFacade" ref="cacheProviderFacade" />
23         <property name="cachingModels"> <!-- 进行cache缓存 -->
24             <props> <!-- 所有StudentService对象中,以get开头的方法都将进行缓存 -->
25                 <prop key="StudentService.get*">cacheName=testCache</prop>
26             </props>
27         </property>
28     </bean>
29     
30     <!-- 缓存刷新拦截器 -->
31     <bean id="flushingInterceptor"
32         class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">
33         <property name="cacheProviderFacade" ref="cacheProviderFacade" />
34         <property name="flushingModels"><!-- 进行cache刷新(清除) -->
35             <props>
36                 <prop key="StudentService.set*">cacheNames=testCache</prop>
37             </props>
38         </property>
39     </bean>
40     
41     <!-- 配置 基于BeanName规则的动态代理封装 -->
42     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
43         <property name="beanNames">
44             <list>
45                 <value>studentService</value>
46             </list>
47         </property>
48         <property name="interceptorNames">
49             <list>
50                 <value>cachingInterceptor</value>
51                 <value>flushingInterceptor</value>
52             </list>
53         </property>
54     </bean>
55 
56     <bean id="studentService" class="StudentService"></bean>
57 </beans>

接下来,为能让EhCache能正常工作,还得编写EhCache配置文件 ehcache.xml, 内容如下:

 1 <ehcache>
 2     <diskStore path="java.io.tmpdir" />
 3     <defaultCache maxElementsInMemory="10000" eternal="false"
 4         timeToIdleSeconds="2" timeToLiveSeconds="5" overflowToDisk="true"
 5         maxElementsOnDisk="10000000" diskPersistent="false"
 6         diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
 7     <cache name="testCache" maxElementsInMemory="10000"
 8         maxElementsOnDisk="1000" eternal="false" overflowToDisk="false"
 9         diskSpoolBufferSizeMB="20" timeToIdleSeconds="60" timeToLiveSeconds="3600"
10         memoryStoreEvictionPolicy="LFU" />
11 </ehcache>


下面将要测试配置是否能正确工作,编写一个测试类 Test.java内容如下:
 1 public class Test {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         AbstractApplicationContext context;
 8         context = new ClassPathXmlApplicationContext("classpath*:context.xml");
 9         context.start();
10         
11         StudentService ss = (StudentService) context.getBean("studentService");
12 
13         String name;
14         System.out.println("第一次访问,没有缓存");
15         name = ss.getName();
16         System.out.println(name);
17         name = ss.getName("Mr");
18         System.out.println(name);
19         
20         //use change not changed value
21         System.out.println("第二次访问,使用缓存");
22         ss.changeNameAndNotTellCache("Michael");
23         name = ss.getName();
24         System.out.println(name);
25         
26         name = ss.getName("Mr");
27         System.out.println(name);
28         
29         //update cache
30         System.out.println("清除缓存后,再次访问 ");
31         ss.setName("Michael");
32         name = ss.getName();
33         System.out.println(name);
34         
35         name = ss.getName("Mr");
36         System.out.println(name);
37         
38         context.close();
39     }
40 
41 }


运行后的输出结果:
第一次访问,没有缓存
matthew
Mr matthew
第二次访问,使用缓存
matthew
Mr matthew
清除缓存后,再次访问 
Michael
Mr Michael

从输出的结果上来看,缓存的功能已经正确启效。
本文只是一个简单的示例,希望对大家有借鉴作用。
更多的资料,详见官方文档 https://springmodules.dev.java.net/.

Good Luck!
Yours Matthew!

posted on 2010-04-22 19:47 x.matthew 阅读(3271) 评论(4)  编辑  收藏 所属分类: Spring|Hibernate|Other framework

只有注册用户登录后才能发表评论。


网站导航: