<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:redis.properties"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<bean id="mutableJedisClientConfiguration" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory$MutableJedisClientConfiguration">
<property name="poolConfig" ref="poolConfig"/>
</bean>
<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg name="thePassword" value="${redis.password}"/>
</bean>
<bean id="standaloneConfig" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="hostName" value="${redis.hostName}"/>
<property name="port" value="${redis.port}"/>
<property name="password" ref="redisPassword"/>
</bean>
<bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="mastername"/>
<constructor-arg name="sentinelHostAndPorts">
<set>
<value>xx.xx.xx.xxx:7001</value>
<value>xx.xx.xx.xxx:7002</value>
<value>xx.xx.xx.xxx:7003</value>
<value>xx.xx.xx.xxx:7004</value>
<value>xx.xx.xx.xxx:7005</value>
<value>xx.xx.xx.xxx:7006</value>
</set>
</constructor-arg>
</bean>
<bean id="clusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<constructor-arg>
<list>
<value>xx.xx.xx.xxx:7001</value>
<value>xx.xx.xx.xxx:7002</value>
<value>xx.xx.xx.xxx:7003</value>
<value>xx.xx.xx.xxx:7004</value>
<value>xx.xx.xx.xxx:7005</value>
<value>xx.xx.xx.xxx:7006</value>
</list>
</constructor-arg>
<property name="password" ref="redisPassword"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="clusterConfiguration"/>
<constructor-arg name="clientConfig" ref="mutableJedisClientConfiguration"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg name="connectionFactory" ref="connectionFactory"/>
</bean>
</beans>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
public class RedisTest {
private StringRedisTemplate redisTemplate;
@Before
public void loadXML() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/redis.xml");
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
}
@Test
public void testRedisString() {
ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
valueOperations.set("zz","ss");
String zzValue = valueOperations.get("zz");
System.out.println(zzValue);
valueOperations.set("time","1");
BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("zz");
boundValueOperations.get();
BoundValueOperations<String,String> boundValueOperationsTime = redisTemplate.boundValueOps("time");
boundValueOperationsTime.increment();
System.out.println(boundValueOperationsTime.get());
}
}