您的位置:首页 > 数据库 > Redis

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: