<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer" > <constructor-arg type="java.lang.Class" value="java.lang.Object"/> </bean>
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:hostName="${redis.hostname}" p:port="${redis.port}" p:password="${redis.password}"/>
</property>或者
不用xml方式配置的话
spring.redis.host=localhostspring.redis.password=spring.redis.port=4555spring.redis.database=0
@Bean public RedisTemplate getInstance(){ RedisTemplate rt=new RedisTemplate(); rt.setKeySerializer(new StringRedisSerializer()); rt.setValueSerializer(new Jackson2JsonRedisSerializer()); return rt; }