SpringMVC集成redis配置的多种实现方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SpringMVC集成redis配置的多种实现⽅法
第⼀步:下载并安装Redis(⽹上已经有很多安装教程在此不细讲了)
<!-- redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
第三步:配置redis.properties⽂件
# Redis Setting
# Redis默认有16个库,序号是0-15,默认是选中的是0号数据库
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端⼝,默认是6379
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
# spring.redis.password=你的密码
# 连接池最⼤阻塞等待时间(使⽤负值表⽰没有限制),根据实际情况修改
spring.redis.pool.maxWaitMillis=-1
# 连接池中的最⼤空闲连接,根据实际情况修改
spring.redis.pool.maxIdle=8
# 连接池中的最⼩空闲连接,根据实际情况修改
spring.redis.pool.minIdle=0
# 连接超时时间(毫秒),根据实际情况修改
spring.redis.timeout=2000
第四步:配置spring-redis-config.xml⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance" xmlns:cache="/schema/cache"
xmlns:context="/schema/context"
xmlns:redis="/schema/redis" xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/cache /schema/cache/spring-cache-4.0.xsd /schema/beans /schema/beans/spring-beans.xsd
/schema/context /schema/context/spring-context-4.0.xsd
/schema/redis /schema/redis/spring-redis-1.0.xsd
/schema/tx /schema/tx/spring-tx-4.0.xsd">
<!-- 载⼊redis.properties,这⾥要特别注意,如果有多个properties⽂件,必须⽤逗号分开,不能写成两个 <context:property-placeholder/> --> <context:property-placeholder location="classpath:redis.properties" />
<!-- 配置JedisPoolConfig连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
<property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
<property name="maxWaitMillis" value="${spring.redis.pool.maxWaitMillis}"></property>
</bean>
<!-- 配置jedis连接⼯⼚ -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"></property>
<property name="hostName" value="${spring.redis.host}"></property>
<property name="port" value="${spring.redis.port}"></property>
<!-- <property name="password" value="${spring.redis.password}"></property> -->
<property name="database" value="${spring.redis.database}"></property>
<property name="timeout" value="${spring.redis.timeout}"></property>
</bean>
<!-- 配置RedisTemplate -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="cacheRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer" />
<property name="hashKeySerializer" ref="stringRedisSerializer" />
<property name="valueSerializer" ref="stringRedisSerializer" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
</bean>
</beans>
第五步:spring集成spring-redis⽂件
⽅式⼀:在spring配置⽂件中加⼊:
<import resource="classpath:spring-redis-config.xml"/>
⽅式⼆:直接将spring-redis-config的东西写到spring配置⽂件⾥。
spring集成Redis基本配置完成!
到此这篇关于SpringMVC集成redis配置的多种实现⽅法的⽂章就介绍到这了,更多相关SpringMVC集成redis配置内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。