springmvc和encache集成关键代码和总结

本文介绍了如何使用ehcache-spring-annotations项目简化Spring与Ehcache的集成,通过注解实现缓存管理。首先引入相关jar包,然后在applicationContext.xml中配置,最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解。文章提供了详细配置示例,并给出了@TriggersRemove注解不同参数下的效果。
摘要由CSDN通过智能技术生成

基于注解的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"/>

更多请见:http://www.mark-to-win.com/tutorial/51896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值