美文网首页spring boot相关SpringBoot之路springBoot+dubbo
spring boot使用总结(四)在spring boot中使

spring boot使用总结(四)在spring boot中使

作者: joyhj | 来源:发表于2016-11-27 16:04 被阅读11094次
    1. 想要在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

    相关文章

      网友评论

      • 这时对那时错:有源码吗...
      • 915f1433e136:你好,请问下redis那些配置去哪了,host,root,pwd等
        joyhj:@假装是程序猿 spring boot 1.4.0.RELEASE
        假装是程序猿:请问一下,用的那个版本的 springboot
        joyhj:@Kevin_Zhan 如果使用Jedis作为客户端且自己不做配置的话,会使用JedisConnectionFactory类中定义的配置,也可以自己自定义修改默认配置

      本文标题:spring boot使用总结(四)在spring boot中使

      本文链接:https://www.haomeiwen.com/subject/kvlnpttx.html