redis在spring框架中的使用
2016-06-29 22:51
489 查看
redis的客户端实现我选择的是jedisspring框架中包含了对redis的支持,其实是一层封装,使得我们可以使用样板来简化代码,也可以选择不使用spring提供的样板,自己在jedis基础上做封装使用spring的项目可以很容易就将redis功能添加进来,作为系统的缓存redis服务端是单独部署的,spring中配置的是redis的客户端,使得spring项目可以访问到redis服务器首先是包的引入,
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency>spring的部分配置,不同的jedis版本,参数也发生了变化,尤其需要注意根据不同的版本使用对应的参数命名
<context:property-placeholder location="classpath:redis.properties" /> <context:component-scan base-package="com.x.redis.dao"> </context:component-scan> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxActive" value="${redis.maxActive}" /> <property name="maxWait" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean>
最后一个bean redisTemplate
通过注入的方式在程序中使用
@Autowiredprotected RedisTemplate<Serializable, Serializable> redisTemplate;
相关文章推荐
- Redis_hase常用命令
- Windows下Redis的安装及PHP扩展使用
- 【Mark】redis入门教程
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resourc
- PHP使用php-resque库配合Redis实现MQ消息队列的教程
- Redis的三种启动方式
- linux安装redis官方教程
- Redis监控方案
- Node.js操作redis示例及常用命令汇总
- redis主从复制
- centos7安装redis3.2.1
- redis 用setbit(bitmap)统计活跃用户
- Python使用Redis实现作业调度系统(超简单)
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
- spring集成redis之RedisTemplate
- CentOS 6.5安装LAMP+Redis+Security.so的整合
- Redis实现简单消息队列
- Redis分布式锁java实现
- redis命令
- Redis学习文章