redis与spring集成--不使用spring-data-redis

本文介绍了如何在Spring中不使用spring-data-redis来集成Redis作为缓存,通过自定义配置和代码实现,避免了JedisShardInfo类构造方法缺失的问题。内容包括redis.xml配置、redisDatasource类的创建以及测试代码的编写。注意在运行时确保Redis服务器已启动,此方法适合简单缓存场景,不适合当作NoSQL数据库使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人感觉如果使用spring-data-redis只作为缓存的话有点累赘,所以有了以下方式

之前在网上搜索了一些资料,但是其中的配置总是报错,原因是JedisShardInfo类缺少相应的构造方法......

1.redis.xml配置

spring的配置文件

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    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="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                	<constructor-arg name="host" value="http://redis:${redis.password}@${redis.host}:${redis.port}"></constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean>
    
    <bean id="data" class="com.x.redis.RedisDataSource">
    	<property name="shardedJedisPool" ref="shardedJedisPool"></property>
    </bean>
    
</beans>

       <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <!-- 这里的http://redis:并不是固定的,只是为了传递参数 -->
                     <constructor-arg name="host" value="http://redis:${redis.password}@${redis.host}:${redis.port}"></constructor-arg>
                </bean>
            </list>
        </constructor-arg>
redis.properties配置

#redis config
redis.host=127.0.0.1
redis.port=6379
redis.password=redis
redis.maxIdle=100 
redis.maxWait=1000 
redis.testOnBorrow=true 
redis.timeout=100000

2.redisDatasource类


为了获取客户端实例方便

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisDataSource
{
	private ShardedJedisPool shardedJedisPool;
	
	/**
	 * spring注入shardedJedisPool
	 * @param shardedJedisPool
	 */
	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool)
	{
		this.shardedJedisPool = shardedJedisPool;
	}
	
	/**
	 * 获取redis客户端ShardedJedis对象
	 * @return
	 */
	public ShardedJedis getRedisClient(){
		try
		{
			return shardedJedisPool.getResource();
		} catch (Exception e)
		{
			System.err.println("get resource error");
		}
		return null;
	}
}

3.测试代码

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.ShardedJedis;

import com.vclouds.redis.RedisDataSource;


public class Sdao
{
	@Test
	public void test(){
		ApplicationContext app = new ClassPathXmlApplicationContext("classpath:redis.xml");
		System.out.println(app);
		ShardedJedis client = app.getBean("data",RedisDataSource.class).getRedisClient();
		System.out.println(client);
		
		client.set("aa", "aaaa");
		System.out.println(client.get("aa"));
	}
	
	
}

4.常见问题

1).代码运行时要保证redis的服务器是启动状态,否则无法连接

2).以上注入方式参考JedisShardInfo类源码

3).个人感觉以上方式使用起来会便捷一些,但是如果将redis作为一个nosql数据库的话,这种方式不推荐






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值