Spring配置redis
来源:互联网 发布:电脑手绘软件 编辑:程序博客网 时间:2024/06/13 10:49
1.引入maven依赖
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency>
2.xml配置
<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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!-- scanner redis properties --> <context:property-placeholder location="/config/redis.properties" /> <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="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean></beans>
3.properties配置
redis.host=127.0.0.1redis.port=6379redis.pass=redis.maxIdle=300redis.maxActive=600redis.maxWait=1000redis.testOnBorrow=true
4.spring引入在applicationContext.xml中添加
<import resource="redis-context.xml"/>
5.使用
package service.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Service;/** * Redis公用类 * @author jinx * */@Servicepublic class RedisService{ @Autowired protected StringRedisTemplate stringRedisTemplate; @Autowired protected RedisTemplate<String,Object> redisTemplate; public String setObj(String key,Object obj){ redisTemplate.opsForValue().set(key,obj); return key; } public String setString(String key,String str){ stringRedisTemplate.opsForValue().set(key,str); return key; } public void clearObj(String key){ redisTemplate.opsForValue().set(key,null); } public String get(String key){ return stringRedisTemplate.opsForValue().get(key); } /** * 取出对象 * @param key * @return */ public Object getObj(String key){ return redisTemplate.opsForValue().get(key); }}
0 0
- SPRING DATA - REDIS配置
- SPRING DATA - REDIS配置
- SPRING DATA - REDIS配置
- redis spring缓存配置
- spring-redis配置
- spring + redis 配置
- spring data redis 配置
- spring redis集群配置
- spring data redis配置
- redis + spring 配置
- spring redis缓存配置
- spring-boot redis 配置
- redis+spring简单配置
- spring项目配置redis
- Spring redis 配置
- Spring配置redis
- spring之redis配置
- redis 哨兵 配置spring
- Android Studio中的图片存放
- 文章标题
- 远程访问ipython notebook
- 运行react-native项目时报错。
- Docker
- Spring配置redis
- 递推递归练习--J(数学黑洞)
- 源于Design Pattern Explanation with C++ Implementation的 设计模式 C++
- Ubuntu16.04下Anaconda,spark,Hadoop的安装
- bzoj4205: 卡牌配对
- 智能指针
- netstat -anp命令简单说明
- Java Web前端到后台常用框架介绍
- 机器学习算法笔记之3:线性模型