redis学习笔记之spring整合redisTemplate

一,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);
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值