redis与spring集成--不使用spring-data-redis
来源:互联网 发布:黑客帝国动画版 知乎 编辑:程序博客网 时间:2024/05/17 01:48
个人感觉如果使用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 configredis.host=127.0.0.1redis.port=6379redis.password=redisredis.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{@Testpublic 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数据库的话,这种方式不推荐
0 0
- redis与spring集成--不使用spring-data-redis
- redis与spring集成--spring-data-redis
- spring-data集成redis
- redis 与Spring Data Redis 使用
- Redis 集成Spring(spring-data-redis)
- Redis 集成Spring(spring-data-redis)
- Spring集成redis(Spring Data Redis)
- Redis 集成Spring(spring-data-redis)
- Redis 集成Spring(spring-data-redis)
- redis与Spring集成
- Spring与redis集成
- redis与spring集成
- spring-data-redis 使用
- spring-data-redis 使用
- Spring Data Redis 使用
- spring data redis使用
- 【spring】spring 与 redis集成
- spring与spring-data-redis整合redis
- OOA、OOD和OOP的定义及之间的关系
- CSS3 pointer-events:none应用举例及扩展
- 关于下拉框option在IE下jquery方法hide()无效的解决办法
- 查看postgres数据库端口
- 模拟数据丢失(truncate table)
- redis与spring集成--不使用spring-data-redis
- springmvc 自定义异常处理机制
- 循环队列之循环队列长度和头尾指针关系
- oracle下lag和lead分析函数
- python post提交json 返回json值
- JAVA虚拟机学习实践2-实践
- iOS9新特性之(OC及Swift)实现3D Touch
- String类练习讲解-统计字符串中出现连续字符的次数
- win apache配置虚拟目录