要实现基于注解的缓存实现,要求Spring的版本在3.1或以上版本。

首先需要在spring的配置文件中添加对缓存注解的实现:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="com.xxx" /> <!-- 启用缓存注解功能 -->
<cache:annotation-driven cache-manager="ehcacheManager"/>
<bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManagerFactory" />
</bean> </beans>

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache name="defaultCache" maxElementsInMemory="300" timeToLiveSeconds="1800"
eternal="false" overflowToDisk="true" /> <cache name="springCache" maxElementsInMemory="300" timeToLiveSeconds="1800"
eternal="false" overflowToDisk="true"/>
</ehcache>

注解的使用:

@Service
public class MyServiceImpl implements MyService {
......
@Cacheable(value="springCache",key="'userlist_' + #param['username']")
public List<Map<String, Object>> queryUserList(Map<String, Object> param)
throws Exception {
return userDAO.queryUserList(param);
}
......
}

  添加了@Cacheable的注解,value属性设为固定值springCache,该值对应ehcache.xml文件中cache的name,key属性设为缓存的名称,可以是一个固定值,比如’userlist’,也可以添加上请求方法的属性值,比如’userlist_’ + #param[‘username’],用于确保输入参数不同,输出的值不同的情况。

  除了@Cacheable注解,还有@CachePut@CacheEvict两个注解,区别在于:

  @Cacheable:如果使用@Cacheable注解,先检查对应的缓存是否存在,如果缓存存在,直接取缓存值返回,不再执行方法体内容;如果缓存不存在,则执行方法体内容,并且将返回的内容写入缓存
  @CachePut:使用@CachePut注解和@Cacheable注解类似,只是不管对应缓存是否存在,都执行方法体,并且将返回的内容写入缓存
  @CacheEvict:@CacheEvict注解表示对应缓存的删除

缓存的使用总结:

  在读取数据时,使用@Cacheable注解将数据加入缓存
  在数据发生改变时,使用@CachePut注解更新缓存
  在数据被删除时,使用@CacheEvict注解删除缓存

spring和ehcache整合,实现基于注解的缓存实现的更多相关文章

  1. spring与hibernate整合配置基于Annotation注解方式管理实务

    1.配置数据源 数据库连接基本信息存放到properties文件中,因此先加载properties文件 <!-- jdbc连接信息 --> <context:property-pla ...

  2. 缓存初解(三)---Spring3.0基于注解的缓存配置+Ehcache和OScache

    本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见: Spring基于注解的缓存配置--web应用实例 一.简介 在spring的modules包中 ...

  3. Spring IoC 源码分析 (基于注解) 之 包扫描

    在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...

  4. 缓存初解(五)---SpringMVC基于注解的缓存配置--web应用实例

    之前为大家介绍了如何使用spring注解来进行缓存配置 (EHCache 和 OSCache)的简单的例子,详见 Spring基于注解的缓存配置--EHCache AND OSCache 现在介绍一下 ...

  5. spring+shiro+ehcache整合

    1.导入jar包(pom.xml文件) <!-- ehcache缓存框架 --> <dependency> <groupId>net.sf.ehcache</ ...

  6. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  7. spring boot整合mybatis基于注解开发以及动态sql的使用

    让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...

  8. Spring IOC容器装配Bean_基于注解配置方式

    bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...

  9. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

随机推荐

  1. Unity3D 中 用quaternion 来对一个坐标点进行旋转的初步体会

    在unity3d中,用四元数来表示旋转,四元数英文名叫quaternion . 比如 transform.rotation 就是一个四元数,其由四个部分组成 Quaternion = (xi + yj ...

  2. 为何重写toString方法后会使哈希码能够打印出来

    首先还是推荐lz看源代码 简单的讲之所以调用了toString()方法,不是什么编译器默认的,而是因为lz你调用的是out.print()方法仔细看源代码,在PringStream类中,print方法 ...

  3. windows下关闭80端口被system占用的情况

    用管理员运行cmd然后用net stop http 停止pid 为4的进程

  4. Android Studio签名打包的两种方式

    签名打包的两种方式: 注:给我们自己开发的app签名,就代表着我自己的版权,以后要进行升级,也必须要使用相同的签名才行.签名就代表着自己的身份(即keystore),多个app可以使用同一个签名. 如 ...

  5. Android性能优化方法(四)

    在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件.一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局.如果我们在 ...

  6. Incompatible operand types DeptE and int 异常处理

    Incompatible operand types DeptE and int 1.java不会运算到==的值,把==改为equals 2.java不会运算到eequals的值 把equals的改为 ...

  7. Android编译错误, Ignoring InnerClasses attribute for an anonymous inner class

    今天在做android项目时,加入第三方包,一编译就报错.错误如下:[2012-01-13 14:51:25 - xxx] Dx warning: Ignoring InnerClasses attr ...

  8. ASP.NET MVC学习之Ajax(完结)

    一.前言 通过上面的一番学习,大家一定收获不少.但是总归会有一个结束的时候,但是这个结束也意味着新的开始. 如果你是从事ASP.NET开发,并且也使用了第三方控件,那么一定会觉得ASP.NET开发aj ...

  9. 用c#开发微信(3)基于Senparc.Weixin框架的接收普通消息处理 (源码下载)

    本文讲述使用Senparc.Weixin框架来快速处理各种接收的普通消息.这里的消息指的是传统的微信公众平台消息交互,微信用户向公众号发送消息后,公众号回复消息给微信用户.包括以下7种类型: 1 文本 ...

  10. Dynamic CRM 2013学习笔记(三十)Linq使用报错 A proxy type with the name account has been defined by another assembly

    在CRM中使用linq时,有时会报这个错误: A proxy type with the name account has been defined by another assembly. Curr ...