介绍
本篇文章给大家分享的是有春天关整合复述,缓存实现以注解的形式使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
maven项目中在砰的一声。xml中依赖2个jar包,其他的春天的jar包省略:
& lt; dependency> & lt; groupId> redis.clients & lt; artifactId> jedis & lt; version> 2.8.1 & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.data & lt; artifactId> spring-data-redis & lt; version> 1.7.2.RELEASE & lt;/dependency>
spring-Redis。xml中的内容:
& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比; http://www.springframework.org/schema/beans" & lt;豆类xmlns=? xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"xmlns: p=癶ttp://www.springframework.org/schema/p" xmlns:上下文=癶ttp://www.springframework.org/schema/context" xmlns: mvc=癶ttp://www.springframework.org/schema/mvc" xmlns:缓存=癶ttp://www.springframework.org/schema/cache" xsi: schemaLocation=? http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/cache ,http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"的在 & lt;上下文:property-placeholder位置=袄嗦肪?redis-config.properties"/比; & lt; !——启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在春天主配置文件中才会生效——比; & lt;缓存:注解驱动的缓存管理器=癱acheManager"/比; & lt; !——复述,相关配置——比; & lt; bean id=皃oolConfig"类=皉edis.clients.jedis.JedisPoolConfig"比; & lt;属性名=癿axIdle"https://www.yisu.com/zixun/value=" $ {redis.maxIdle} "/> <属性名=" maxWaitMillis " value=" ${复述。maxWait} "/> <属性名=" testOnBorrow " value=" ${复述。testOnBorrow} "/> 豆><属性名=" connectionFactory“ref=" JedisConnectionFactory "/> 豆> <属性名=盎捍妗? <设置> <属性名=" redisTemplate“ref=" redisTemplate "/> <属性名="名称" value="默认"/> 豆> - -> <属性名=" redisTemplate“ref=" redisTemplate "/> <属性名="名称" value="共同"/> 豆> 设置> 属性 豆> 豆>
redis-config。中属性的内容:
#复述,设置 #服务器IP redis.host=127.0.0.1 #服务器端口 redis.port=6379 #服务器通过 redis.pass=#使用dbIndex redis.database=0 #控制一个池最多有多少个状态为闲置(空闲的)的能实例 redis.maxIdle=300 #表示当借(引入)一个能实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException; redis.maxWait=3000 #在借一个能实例时,是否提前进行验证操作,如果为真,则得到的能实例均是可用的 redis.testOnBorrow=true
com.cn.util.RedisCache类中的内容:
包com.cn.util; 进口java.io.ByteArrayInputStream; 进口java.io.ByteArrayOutputStream; 进口java.io.IOException; 进口java.io.ObjectInputStream; 进口java.io.ObjectOutputStream; 进口org.springframework.cache.Cache; 进口org.springframework.cache.support.SimpleValueWrapper; 进口org.springframework.dao.DataAccessException; 进口org.springframework.data.redis.connection.RedisConnection; 进口org.springframework.data.redis.core.RedisCallback; 进口org.springframework.data.redis.core.RedisTemplate; 公共类RedisCache实现缓存{ 私人RedisTemplate春天整合复述,缓存实现以注解的形式使用