【spring】spring 与 redis集成

2 篇文章 0 订阅
1 篇文章 0 订阅

spring与redis集成有两种,一种是通过spring-data来集成,还有一种是通过 jedis,第一种集成比较简单,第二种自由度更大,本文用的是第一种方式

除spring的jar包,还需要的依赖

  • pom.xml

<!--redis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.2</version>
</dependency>

<!-- spring-redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.6.4.RELEASE</version>
</dependency>

spring集成redis的配置

  • spring-context-redis.xml
<?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" file-encoding="UTF-8" ignore-unresolvable="true" />

    <!--扫描bean-->
    <context:component-scan base-package="xyz.mrwood.spring.integration.example.redis" />

    <!-- redis配置bean -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>
    <!--redis连接类-->
    <bean id="connectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

    <!--泛型redis模板类-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>

    <!-- 字符型redis模板类 -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

</beans>

配置文件

  • redis.properties
########### reids配置 ############
#ip
redis.host=127.0.0.1
#port
redis.port=6379
#密码
#redis.pass=myredis123
#最大能够保持idel状态的对象数
redis.maxIdle=200
#最大分配的对象数
redis.maxTotal=512
#当池内没有返回对象时,最大等待时间
redis.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.testOnBorrow=true
########### reids配置 ############

测试代码

/**
 * Copyright (c) 2016, 791650277@qq.com(Mr.kiwi) All Rights Reserved.
 */
package xyz.mrwood.spring.integration.example.redis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * 项目:spring-integration-example
 * 包名:xyz.mrwood.spring.integration.example.redis
 * 功能:
 * 作者:Mr.Kiwi
 */
public class TestRedis {

    private ApplicationContext applicationContext = null;

    @BeforeMethod
    public void loadApplication(){

        applicationContext =
            new ClassPathXmlApplicationContext("spring-context-redis.xml");



    }

    @Test
    public void testOpsForValue(){

        // 取出模板类
        StringRedisTemplate stringRedisTemplate =
            applicationContext.getBean("stringRedisTemplate", StringRedisTemplate.class);

        // 设值
        stringRedisTemplate.opsForValue().set("name", "kiwi");


        // 取值
        String name = stringRedisTemplate.opsForValue().get("name");
        System.err.println(name);
    }

    @Test
    public void testExpire(){

        // 取出模板类
        StringRedisTemplate stringRedisTemplate =
            applicationContext.getBean("stringRedisTemplate", StringRedisTemplate.class);

//        stringRedisTemplate.expire("name", 10, TimeUnit.SECONDS);
        Long expireTime = stringRedisTemplate.getExpire("name");
        System.err.println(expireTime);

        System.err.println(stringRedisTemplate.opsForValue().get("name"));
    }

}

项目源码
spring与redis集成

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值