spring与redis简单整合
2016-12-14 16:42
369 查看
项目结构
整合需要的依赖
redis.properties
spring-redis.xml
spring-web.xml
测试
整合需要的依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
redis.properties
redis.hostName=127.0.0.1 redis.port=6379 redis.timeout=15000 redis.usePool=true redis.maxIdle=6 redis.minEvictableIdleTimeMillis=300000 redis.numTestsPerEvictionRun=3 redis.timeBetweenEvictionRunsMillis=60000
spring-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}"></property> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"></property> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"></property> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="poolConfig" ref="jedisPoolConfig"></property> <property name="hostName" value="${redis.hostName}"></property> <property name="port" value="${redis.port}"></property> <property name="timeout" value="${redis.timeout}"></property> <property name="usePool" value="${redis.usePool}"></property> </bean> </beans>
spring-web.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"></property> </bean> </beans>
测试
package com.winner.redis; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.StringRedisTemplate; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by winner_0715 on 2016/12/24. */ public class RedisTest { @Test public void testSpringRedis() { ClassPathXmlApplicationContext ctx = null; try { ctx = new ClassPathXmlApplicationContext("spring/spring.xml"); StringRedisTemplate stringRedisTemplate = ctx.getBean("stringRedisTemplate", StringRedisTemplate.class); // String读写 stringRedisTemplate.delete("myStr"); stringRedisTemplate.opsForValue().set("myStr", "http://yjmyzz.cnblogs.com/"); System.out.println(stringRedisTemplate.opsForValue().get("myStr")); System.out.println("---------------"); // List读写 stringRedisTemplate.delete("myList"); stringRedisTemplate.opsForList().rightPush("myList", "A"); stringRedisTemplate.opsForList().rightPush("myList", "B"); stringRedisTemplate.opsForList().leftPush("myList", "0"); List<String> listCache = stringRedisTemplate.opsForList().range( "myList", 0, -1); for (String s : listCache) { System.out.println(s); } System.out.println("---------------"); // Set读写 stringRedisTemplate.delete("mySet"); stringRedisTemplate.opsForSet().add("mySet", "A"); stringRedisTemplate.opsForSet().add("mySet", "B"); stringRedisTemplate.opsForSet().add("mySet", "C"); Set<String> setCache = stringRedisTemplate.opsForSet().members( "mySet"); for (String s : setCache) { System.out.println(s); } System.out.println("---------------"); // Hash读写 stringRedisTemplate.delete("myHash"); stringRedisTemplate.opsForHash().put("myHash", "PEK", "北京"); stringRedisTemplate.opsForHash().put("myHash", "SHA", "上海虹桥"); stringRedisTemplate.opsForHash().put("myHash", "PVG", "浦东"); Map<Object, Object> hashCache = stringRedisTemplate.opsForHash() .entries("myHash"); for (Map.Entry<Object, Object> entry : hashCache.entrySet()) { System.out.println(entry.getKey() + " - " + entry.getValue()); } System.out.println("---------------"); } finally { if (ctx != null && ctx.isActive()) { ctx.close(); } } } }
相关文章推荐
- spring简单整合Redis配置
- 【spring boot】SpringBoot初学(8)– 简单整合redis
- redis整合spring实现简单的操作
- SpringBoot 整合 Redis 的简单案例
- spring整合redis demo 简单入门
- spring 整合redis 简单使用
- jedis与spring整合及简单的使用RedisTemplate操作
- spring整合redis简单demo
- SpringBoot 整合 Redis 的简单案例
- Spring Boot整合集成StringRedisTemplate的简单记录
- 简单示例:Spring4 整合 单个Redis服务
- Java爬虫初体验:简单抓取IT之家热评(整合Spring Boot+Elasticsearch+Redis+Mybatis)
- Spring简单整合redis
- spring 整合redis的简单操作
- jedis与spring整合及简单的使用RedisTemplate操作
- jedis与spring整合及简单的使用RedisTemplate操作
- spring整合redis简单demo
- 整合开发Struts2,Hibernate,Spring简单框架的搭建详解
- MyEclipse整合SSH(Struts+Spring+Hibernate)简单登录范例(一)
- Spring整合Struts2简单示例(转)