`

利用Spring AOP 缓存方法结果集(下)

阅读更多

invoke方法中,首先根据key查询缓存(key=className + methodName + arguments)

,缓存中存在则返回,否之调用invocation.proceed()返回结果.

Spring配置文件中定义拦截器

<bean id="methodCacheInterceptor" class="org.taha.interceptor.MethodCacheInterceptor">

  <property name="cache">

    <ref local="methodCache" />

  </property>

</bean>

 

<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">

  <property name="advice">

    <ref local="methodCacheInterceptor"/>

  </property>

  <property name="patterns">

    <list>

      <value>.*methodOne</value>

      <value>.*methodTwo</value>

    </list>

  </property>

</bean>

 

<bean id="myBean" class="org.springframework.aop.framework.ProxyFactoryBean">

  <property name="target">

   <bean class="org.taha.beans.MyBean"/>

  </property>

  <property name="interceptorNames">

    <list>

      <value>methodCachePointCut</value>

    </list>

  </property>

</bean>

这里org.springframework.aop.support.RegexpMethodPointcutAdvisor是一个正规表达式切入点,

使用Perl 5的正规表达式的语法, Jakarta ORO(有空写个文档,自己研究一下).

  <property name="target">

   <bean class="org.taha.beans.MyBean"/>

  </property>

org.taha.beans.MyBean是我们需要做缓存处理的类.

methodCachePointCut

<value>.*methodOne</value>

<value>.*methodTwo</value>

则是指定的模式匹配方法,对应于org.taha.beans.MyBean中的方法. 这里指定了2个方法需要做缓存处理.

呵呵,就是这么简单.这样每次对org.taha.beans.MyBeanmethodOne方法进行调用,都会首先从缓存查找,

其次才会查询数据库. 这样我就不需要在xx.hbm.xml来指定讨厌的cache.也不需要在开发阶段来关心缓存.

一切AOP搞定.. ^_^

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics