`

Spring使用Cache(三)

 
阅读更多

2       配置SpringCache的支持

2.1     声明对Cache的支持

2.1.1  基于注解

       配置Spring对基于注解的Cache的支持,首先我们需要在Spring的配置文件中引入cache命名空间,其次通过<cache:annotation-driven />就可以启用Spring对基于注解的Cache的支持。

<?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:cache="http://www.springframework.org/schema/cache"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

     http://www.springframework.org/schema/cache

     http://www.springframework.org/schema/cache/spring-cache.xsd">

 

   <cache:annotation-driven/>

 

</beans>

 

       <cache:annotation-driven/>有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager,所以当我们的CacheManageridcacheManager时我们可以不指定该参数,否则就需要我们指定了。

       <cache:annotation-driven/>还可以指定一个mode属性,可选值有proxyaspectj。默认是使用proxymodeproxy时,只有缓存方法在外部被调用的时候Spring Cache才会发生作用,这也就意味着如果一个缓存方法在其声明对象内部被调用时Spring Cache是不会发生作用的。而modeaspectj时就不会有这种问题。另外使用proxy时,只有public方法上的@Cacheable等标注才会起作用,如果需要非public方法上的方法也可以使用Spring Cache时把mode设置为aspectj

       此外,<cache:annotation-driven/>还可以指定一个proxy-target-class属性,表示是否要代理class,默认为false。我们前面提到的@Cacheable@cacheEvict等也可以标注在接口上,这对于基于接口的代理来说是没有什么问题的,但是需要注意的是当我们设置proxy-target-classtrue或者modeaspectj时,是直接基于class进行操作的,定义在接口上的@CacheableCache注解不会被识别到,那对应的Spring Cache也不会起作用了。

       需要注意的是<cache:annotation-driven/>只会去寻找定义在同一个ApplicationContext下的@Cacheable等缓存注解。

 

2.1.2  基于XML配置

       除了使用注解来声明对Cache的支持外,Spring还支持使用XML来声明对Cache的支持。这主要是通过类似于aop:advicecache:advice来进行的。在cache命名空间下定义了一个cache:advice元素用来定义一个对于Cacheadvice。其需要指定一个cache-manager属性,默认为cacheManagercache:advice下面可以指定多个cache:caching元素,其有点类似于使用注解时的@Caching注解。cache:caching元素下又可以指定cache:cacheablecache:cache-putcache:cache-evict元素,它们类似于使用注解时的@Cacheable@CachePut@CacheEvict。下面来看一个示例:

   <cache:advice id="cacheAdvice" cache-manager="cacheManager">

      <cache:caching cache="users">

         <cache:cacheable method="findById" key="#p0"/>

         <cache:cacheable method="find" key="#user.id"/>

         <cache:cache-evict method="deleteAll" all-entries="true"/>

      </cache:caching>

   </cache:advice>

 

       上面配置定义了一个名为cacheAdvicecache:advice,其中指定了将缓存findById方法和find方法到名为users的缓存中。这里的方法还可以使用通配符“*”,比如“find*”表示任何以“find”开始的方法。

       有了cache:advice之后,我们还需要引入aop命名空间,然后通过aop:config指定定义好的cacheAdvice要应用在哪些pointcut上。如:

   <aop:config proxy-target-class="false">

      <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.xxx.UserService.*(..))"/>

   </aop:config>

       上面的配置表示在调用com.xxx.UserService中任意公共方法时将使用cacheAdvice对应的cache:advice来进行Spring Cache处理。更多关于Spring Aop的内容不在本文讨论范畴内。

 

2.2     配置CacheManager

       CacheManagerSpring定义的一个用来管理Cache的接口。Spring自身已经为我们提供了两种CacheManager的实现,一种是基于Java APIConcurrentMap,另一种是基于第三方Cache实现——Ehcache,如果我们需要使用其它类型的缓存时,我们可以自己来实现SpringCacheManager接口或AbstractCacheManager抽象类。下面分别来看看Spring已经为我们实现好了的两种CacheManager的配置示例。

2.2.1  基于ConcurrentMap的配置

   <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">

      <property name="caches">

         <set>

            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="xxx"/>

         </set>

      </property>

   </bean>

       上面的配置使用的是一个SimpleCacheManager,其中包含一个名为“xxx”的ConcurrentMapCache

 

2.2.2  基于Ehcache的配置

   <!-- Ehcache实现 -->

   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheManager"/>

   <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache-spring.xml"/>

       上面的配置使用了一个Spring提供的EhCacheCacheManager来生成一个SpringCacheManager,其接收一个EhcacheCacheManager,因为真正用来存入缓存数据的还是EhcacheEhcacheCacheManager是通过Spring提供的EhCacheManagerFactoryBean来生成的,其可以通过指定ehcache的配置文件位置来生成一个EhcacheCacheManager。若未指定则将按照Ehcache的默认规则取classpath根路径下的ehcache.xml文件,若该文件也不存在,则获取Ehcache对应jar包中的ehcache-failsafe.xml文件作为配置文件。更多关于Ehcache的内容这里就不多说了,它不属于本文讨论的内容,欲了解更多关于Ehcache的内容可以参考我之前发布的Ehcache系列文章,也可以参考官方文档等

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics