- 想要在spring boot中使用redis,只要在pom文件中增加如下的配置就可以了。
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>```
2. 引入spring-boot-redis jar包之后,选择相应的客户端配置连接就可以使用redis了,推荐使用Jedis。
```@Bean
JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}```
3. 配置完连接后,就可以使用redis进行简单的存取了。如果存取的key,value都是String的,可以使用
RedisTemplate<String, String>对象,该对象由spring boot自动扫描生成,在需要的地方可以直接注入。如果只是进行简单的存储,还可以生成
ValueOperations对象来方便操作。
```@Bean
ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}```
4. 如果想要存取其他类型的对象,例如存取key为string,value为Integer类型的对象,可以new这个对象,然后配置连接即可使用。
```@Bean
RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) {
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>();
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}```
5. RedisTemplate对象默认使用jdkSerializer实现序列化,如果想要更换序列化的实现方式,例如使用json实现value的序列化,可以进行如下配置
```@Bean
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}
@Bean
RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory,
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
return redisTemplate;
}
最后附上上述操作的完整代码
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
@Bean
ValueOperations<String, String> strOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
RedisTemplate<String, Integer> intRedisTemplate(JedisConnectionFactory connectionFactory) {
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>();
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
ValueOperations<String, Integer> intOperations(RedisTemplate<String, Integer> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}
@Bean
RedisTemplate<String, Object> objRedisTemplate(JedisConnectionFactory connectionFactory,
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
return redisTemplate;
}
@Bean
ValueOperations<String, Object> objOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
}```
参考文档:
1. http://docs.spring.io/spring-data/redis/docs/current/reference/html/
2. http://docs.spring.io/spring-framework/docs/4.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html
3. http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html