springboot 使用ValueOperations在启动类中添加

在主启动类中添加

@Bean(na me="valueOperations")

public ValueOperations valueOperations(){

RedisConnectionFactory  RedisConnectionFactory = new RedisConnectionFactory();

RedisTemplate redistemplate = new RedisTemplate();

redisTemplate.setConnectionFactory(RedisConnectionFactory);

ValueOperations valueOperations =redisTemplate.opsForValue();

return valueOperation;

}

在serviceImpl实现类中注入

@Resoure(name="redisTemplate")

public ValueOperations valueOperations

  1. 想要在spring boot中使用redis,只要在pom文件中增加如下的配置就可以了。
    org.springframework.boot
    spring-boot-starter-data-redis
```

2. 引入spring-boot-redis jar包之后,选择相应的客户端配置连接就可以使用redis了,推荐使用Jedis。
```@Bean
JedisConnectionFactory connectionFactory() {
    return new JedisConnectionFactory();
}```

3. 配置完连接后,就可以使用redis进行简单的存取了。如果存取的key,value都是String的,可以使用
RedisTemplate对象,该对象由spring boot自动扫描生成,在需要的地方可以直接注入。如果只是进行简单的存储,还可以生成
ValueOperations对象来方便操作。
```@Bean
ValueOperations strOperations(RedisTemplate redisTemplate) {
    return redisTemplate.opsForValue();
}```

4. 如果想要存取其他类型的对象,例如存取key为string,value为Integer类型的对象,可以new这个对象,然后配置连接即可使用。
```@Bean
RedisTemplate intRedisTemplate(JedisConnectionFactory connectionFactory) {
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(connectionFactory);
    return redisTemplate;
}```

5. RedisTemplate对象默认使用jdkSerializer实现序列化,如果想要更换序列化的实现方式,例如使用json实现value的序列化,可以进行如下配置

```@Bean
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
            Object.class);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    return jackson2JsonRedisSerializer;
}

@Bean
RedisTemplate objRedisTemplate(JedisConnectionFactory connectionFactory,
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {
    RedisTemplate redisTemplate = new RedisTemplate();
    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 strOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    RedisTemplate intRedisTemplate(JedisConnectionFactory connectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }

    @Bean
    ValueOperations intOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(
                Object.class);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

    @Bean
    RedisTemplate objRedisTemplate(JedisConnectionFactory connectionFactory,
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        return redisTemplate;
    }

    @Bean
    ValueOperations objOperations(RedisTemplate 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/springf 
  

 

你可能感兴趣的:(springboot,redis)