简介:
spring整合redis,废话不多说直接上代码!
参考:http://docs.spring.io/spring-data/redis/docs/1.7.2.RELEASE/reference/html
http://projects.spring.io/spring-data-redis/
流程:
pom.xml
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> </dependencies>
applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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: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">
<context:property-placeholder location="classpath:redis.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:maxIdle="${redis.maxIdle}"
p:maxTotal="${redis.maxActive}"
p:maxWaitMillis="${redis.maxWait}"
p:testOnBorrow="${redis.testOnBorrow}">
</bean>
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:usePool="true"
p:hostName="127.0.0.1"
p:port="6379"
p:timeout="200"
p:poolConfig-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" >
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="enableTransactionSupport" value="true" />
</bean>
<bean id="redisDao" class="com.test.redis.RedisDao" >
<constructor-arg name="redisTemplate" ref="redisTemplate"/>
</bean>
</beans>
RedisDao.java
package com.test.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Created by zz on 2016/9/8.
*/
public class RedisDao {
@Autowired
protected RedisTemplate<String, String> redisTemplate;
public RedisDao(RedisTemplate<String, String> redisTemplate){
this.redisTemplate = redisTemplate ;
}
public void insert(final String key,final String value){
this.redisTemplate.opsForValue().set(key, value);
}
public String getByKey(final String key){
return this.redisTemplate.opsForValue().get(key);
}
public void delKey(final String key){
this.redisTemplate.delete(key);
}
}
App.java
package com.test.redis;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by zz on 2016/9/8.
*/
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-redis.xml");
System.out.println("start secceed!");
RedisDao redisDao =(RedisDao)ctx.getBean("redisDao");
redisDao.insert("one","is 1");
// redisDao.delKey("one");
String one = redisDao.getByKey("one") ;
System.out.println(one);
}
}
ok.最后将在win64上安装redis:
1.从https://github.com/MSOpenTech/redis/releases这里下载
2.在dos中D:\>redis-server.exe redis.windows-service.conf 启动即可,没有任何提示
3.双击执行redis-cli.exe 弹出黑框,输入get key即可
Spring整合Redis实战

本文介绍如何使用Spring Data Redis实现Spring应用与Redis的整合。通过配置pom.xml引入所需依赖,并设置applicationContext-redis.xml文件来连接Redis服务器。此外,还提供了一个简单的RedisDao类用于增删查操作。

被折叠的 条评论
为什么被折叠?



