Spring基于XML配置的Cache支持

配置 ehcacheContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:task="http://www.springframework.org/schema/task"
	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.1.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
	http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
	http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
	default-autowire="byName"  default-init-method="init">
    <!-- 在spring里配置cache就和在spring配置数据库一样, -->  
    <!-- 开启切面注解 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <!--  缓存  属性-->  
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
        <property name="configLocation"  value="classpath:ehcache.xml"/>   
    </bean>   
      
    <!-- 默认是cacheManager -->  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    
        <property name="cacheManager"  ref="cacheManagerFactory"/>    
    </bean>
  
  	<!-- 支持缓存注解 -->  
    <cache:annotation-driven cache-manager="cacheManager" />
    
    <cache:advice id="cacheAdvice" cache-manager="cacheManager">
      <cache:caching cache="billingCache1">
      	 <cache:cacheable method="query*" key="#root.methodName + #p0"/>
         <cache:cache-evict method="delete*" all-entries="true"/>
         <cache:cache-evict method="update*" all-entries="true"/>
      </cache:caching>
   </cache:advice>
    
    <aop:config proxy-target-class="true">
      <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.mobile263.billing.service.*.*(..))"/>
   </aop:config>
   
    
</beans>

Spring为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。


属性名称

描述

示例

methodName

当前方法名

#root.methodName

method

当前方法

#root.method.name

target

当前被调用的对象

#root.target

targetClass

当前被调用的对象的class

#root.targetClass

args

当前方法参数组成的数组

#root.args[0]

caches

当前被调用的方法使用的Cache

#root.caches[0].name


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值