以下为Maven的pom.xml配置文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cfwx</groupId> <artifactId>spring-data-redis-tutorial</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-data-redis-tutorial</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.0.RC1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> </dependencies> <repositories> <!-- used for nightly builds --> <repository> <id>spring-maven-snapshot</id> <snapshots> <enabled>true</enabled> </snapshots> <name>Springframework Maven SNAPSHOT Repository</name> <url>http://repo.springsource.org/libs-release</url> </repository> <!-- used for milestone/rc releases --> <repository> <id>spring-maven-milestone</id> <name>Springframework Maven Milestone Repository</name> <url>http://repo.springsource.org/libs-milestone</url> </repository> </repositories> </project>
以下为Spring的配置文件applicationContext.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:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:component-scan base-package="com.suxianbin.redis.tutorial" /> <!-- Jedis ConnectionFactory --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="192.168.1.123" p:port="6390" /> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" /> </beans>
项目的结构:
测试用例的源码:
package com.suxianbin.redis.tutorial; import java.util.HashMap; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/applicationContext.xml" }) public class ExampleTest { @Autowired private RedisTemplate<String, HashMap<String, String>> redisTemplate; @Test public void testSaveValue() { String key = "myKey"; HashMap<String, String> value = new HashMap<String, String>(); value.put("Name", "XianBin"); redisTemplate.opsForValue().set(key, value); Object obj = redisTemplate.opsForValue().get(key); System.out.println("Object:" + obj.toString()); } }
更多有关Spring Data Redis项目的信息请访问:
http://www.springsource.org/spring-data/redis