基于注解的spring缓存配置 了解更多与spring集成的Echache 还有分布式的mencache让我更好学习了oscache
介绍二者集成之前,先介绍下GoogleCode上的ehcache-spring-annotations项目
/**
* ehcache-spring-annotations简介
* @see ----------------------------------------------------------------------------------------------------------------
* @see 关于Spring与Ehcache的集成,googlecode上有一个经Apache认证的开源项目叫做ehcache-spring-annotations
* @see 目前已经到了1.2.0版本,它只是简化了Spring和Ehcache集成的复杂性(事实上我觉得完全没必要,因为它俩集成并不复杂)
* @see 尽管如此还是要提一下,它的项目主页为https://code.google.com/p/ehcache-spring-annotations/
* @see 这篇文章中描述了其用法http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/
* @see ----------------------------------------------------------------------------------------------------------------
* @see 1)使用时在项目中引入ehcache-spring-annotations-1.2.0.jar和guava-r09.jar两个jar文件
* @see 2)然后在applicationContext.xml按照如下配置
* @see <beans xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
* @see xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
* @see http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">
* @see <ehcache:annotation-driven/>
* @see <ehcache:config cache-manager="cacheManager">
* @see <ehcache:evict-expired-elements interval="60"/>
* @see </ehcache:config>
* @see <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
* @see <property name="configLocation" value="classpath:ehcache.xml"/>
* @see </bean>
* @see 3)最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解即可
* @see 经我亲测,@TriggersRemove(cacheName="..", when="..", removeAll=true)可移除缓存中的全部对象
* @see 但若写成@TriggersRemove(cacheName="..", when="..")则不会移除缓存中的单一的或所有的对象,即缓存中的对象无变化
* @see ----------------------------------------------------------------------------------------------------------------
* @create Oct 3, 2013 4:56:35 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/
下面开始罗列示例代码,首先是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- SpringMVC核心分发器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
然后是//src//applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.jadyer"/>
<!-- SpringMVC配置 -->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/" view-name="forward:/index.jsp"/>