使用spring访问redis

使用spring访问redis

一、引入spring提供的访问redis的api的jar包

修改pom.xml文件(使用maven管理依赖),添加如下配置:

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

二、修改配置文件

1、配置redis相关bean

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.development.properties</value>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

    <!-- redis config -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive" value="${redis.pool.maxActive}" />
        <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <property name="maxWait" value="${redis.pool.maxWait}" />
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.server}"/>
        <property name="port" value="${redis.port}"/>
        <property name="timeout" value="${redis.timeout}" />  
        <property name="poolConfig" ref="jedisPoolConfig" />  
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="KeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <property name="ValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property> 
    </bean>

2、redis参数设置

redis.pool.maxActive=100
redis.pool.maxIdle=50
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true

redis.timeout=50000
redis.server=127.0.0.1
redis.port=6379

三、单元测试代码


import static org.junit.Assert.assertNotNull;

import java.io.Serializable;

import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration({ "classpath:beans.xml" })
public class RedisTest extends AbstractJUnit4SpringContextTests {

    @Resource
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    @Test
    public void test() {
        assertNotNull(redisTemplate);
        // 获取redis缓存数据
        assertNotNull(redisTemplate.opsForValue().get("conn_test_name"));
        System.out.println(redisTemplate.opsForValue().get("conn_test_name"));

    }

}

四、启动redis服务

1、server

redis-server
redis 127.0.0.1:6379> keys *
(empty list or set)
redis 127.0.0.1:6379> set conn_test_name for_test_value
OK
redis 127.0.0.1:6379> keys *
1) “conn_test_name”

2、client

redis-cli -h 127.0.0.1
redis 127.0.0.1:6379> get conn_test_name
“for_test_value”

五、最后

运行junit即可,junit运行成功。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值