为什么要学习在spring中使用redis,一开始我们是用jedis去操作redis的,但是redis只提供基于字符串的操作,而在java中使用的却是以类对象为主,所以需要redis存储的字符串和java对象相互转化。如果自己编写规则,很麻烦,而spring中封装了这些东西,还提供了工具类,所以学习在spring中使用redis,很有必要。
1.准备的东西,使用的是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.taylor</groupId>
<artifactId>Spring-redis</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
2.完善applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.2.xsd">
<!--spring的配置代码 -->
<!--首先配置JedisPoolConfig对象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="100"/>
<!-- 最大连接数 -->
<property name="maxTotal" value="200"/>
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000"/>
</bean>
<!--配置JedisConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<!-- <property name="password" value=""/> -->
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- 配置spring RedisTemplate -->
<!--键序列器 -->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!-- 值序列器-->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>
3.测试代码和运行截图
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import com.taylor.pojo.Role;
public class TestJedis {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate =applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("taylor");
role.setNote("hello");
redisTemplate.opsForValue().set("role_1", role);
Role role1 = (Role) redisTemplate.opsForValue().get("role_1");
System.out.println(role1.getRoleName());
//以上代码存在问题,不能保证set操作和get操作来自于同一个连接
/*
* 为了使得所有的操作来自于同一个连接,可以使用SessionCallback或者是RedisCallback
* 这两个接口,更多的时候还是会用到SessionCallback
*/
}
@Test
public void fun() {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate =applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("swift");
role.setNote("hello");
//前面的操作都是一样,只是把set和get方法放到了接口中
SessionCallback callback = new SessionCallback<Role>() {
@Override
public Role execute(RedisOperations operations) throws DataAccessException {
operations.boundValueOps("role_1").set(role);
return (Role) operations.boundValueOps("role_1").get();
}
};
//使用了SessionCallback,这样可以保证在同一个连接池的同一个Redis连接进行操作。
Role role1 = (Role) redisTemplate.execute(callback);
System.out.println(role1.getRoleName());
}
}
4.问题
(1)如果发现连接不上,检测是否开启redis服务。
(2)如果是spring内部对象创建异常,检测配置文件的正确性。
5.源码文件
链接:https://pan.baidu.com/s/1uoHTKyRaqPuwUzsjoN970Q 密码:l2mk