鉴于redis版本更新很快,网上的例子基本都是说版本。这里弄了一个比较新的配置redis方式。
新版本的redsi配置基本都是构造函数注入。有兴趣的同学可以深入源码了解,这里只用了最简单的一种配置方式。
<?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:context="http://www.springframework.org/schema/context"
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">
<context:property-placeholder location="classpath:redis.properties"/>
<!-- jedis 配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲连接数, 默认8个-->
<property name="maxIdle" value="${redis.maxIdle}"/>
<!--获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常,小于零:阻塞不确定的时间,默认-1-->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--在获取连接的时候检查有效性, 默认false-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="standaloneConfig" ref="standaloneConfig"/>
<constructor-arg name="clientConfig">
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory.MutableJedisClientConfiguration">
<property name="poolConfig" ref="poolConfig"/>
</bean>
</constructor-arg>
</bean>
<bean id="standaloneConfig" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<constructor-arg name="hostName" value="172.16.4.170"/>
<constructor-arg name="port" value="6379"/>
</bean>
<!-- 对象序列化方式 -->
<bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<!--想使用json,要依赖json的3个包,jackson-core,jackson-annotationsjackson-databind-->
<bean id="jackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer" ref="stringSerializer"/>
<property name="hashKeySerializer" ref="stringSerializer"/>
<property name="valueSerializer" ref="jackson2JsonRedisSerializer"/>
<property name="hashValueSerializer" ref="jackson2JsonRedisSerializer"/>
</bean>
</beans>