一,pom.xml中加入以下依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
二,完成关于redisTemplate的配置
<?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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
">
<!--JedisConnectionFactory 使用 public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig)完成实例化-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!--为RedisStandaloneConfiguration完成实例化-->
<constructor-arg name="standaloneConfig">
<bean class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<!--通过源码发现密码需要RedisPassword这个对象中的静态方法of来完成配置-->
<property name="password">
<!--所以使用spring提供的RedisStandaloneConfiguration类来调用静态方法-->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!--属性staticMethod为要调用的静态方法的全路径名-->
<property name="staticMethod"
value="org.springframework.data.redis.connection.RedisPassword.of"/>
<!--属性arguments为方法需要的参数-->
<property name="arguments" value="xiangy"/>
</bean>
</property>
<!--配置主机名字(IP地址)-->
<property name="hostName" value="192.168.227.129"/>
<!--配置端口号-->
<property name="port" value="6379"/>
<!--选择数据库(通过源码发现默认为0)-->
<property name="database" value="0"/>
</bean>
</constructor-arg>
<!--为JedisClientConfiguration完成实例化-->
<constructor-arg name="clientConfig">
<!--JedisClientConfiguration为接口所以使用实现类MutableJedisClientConfiguration-->
<bean id="configuration"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory.MutableJedisClientConfiguration">
<!--连接池配置-->
<property name="poolConfig">
<bean class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="3"/>
<property name="minIdle" value="1"/>
<property name="maxTotal" value="5"/>
</bean>
</property>
</bean>
</constructor-arg>
</bean>
<!--实例化一个字符串序列化规则-->
<bean id="redisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!--redisTemplate实例-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--引用上面配置好的jedisConnectionFactory-->
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<!--统一字符串操作序列化规则(
注意:1.如果使用默认规则,会有一坨编码前缀
\xac\xed\x00\x05t\x00\x06username
2.如果keySerializer和valueSerializer不一致,则有可能会报错
Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?;
)-->
<property name="keySerializer" ref="redisSerializer"/>
<property name="valueSerializer" ref="redisSerializer"/>
</bean>
</beans>
三,测试
public class TestTemplate {
private RedisTemplate redisTemplate;
@Before
public void instance(){
redisTemplate = new ClassPathXmlApplicationContext("application-redis.xml").getBean(RedisTemplate.class);
}
@Test
public void testString(){
Set keys = redisTemplate.keys("*");
for (Object key : keys) {
System.out.println(key);
}
}
}