美文网首页
springboot自学笔记-整合redis(二)

springboot自学笔记-整合redis(二)

作者: Hush____ | 来源:发表于2018-12-19 16:44 被阅读5次

    基于整合redis(一)的基础上,将连接池信息和redis连接信息整合到配置文件application.properties中。

    整合redis的关键类:

    package com.test.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        public JedisPoolConfig jedisPoolConfig() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            //最大空闲数量
            jedisPoolConfig.setMaxIdle(10);
            //最小空闲数量
            jedisPoolConfig.setMinIdle(5);
            //最大连接数量
            jedisPoolConfig.setMaxTotal(30);
            return jedisPoolConfig;
        }
        
        @Bean
        public JedisConnectionFactory jedisPoolConfig(JedisPoolConfig config) {
            JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory();
            //关联连接池的配置对象
            JedisConnectionFactory.setPoolConfig(config);
            //配置连接redis的信息
            JedisConnectionFactory.setHostName("127.0.0.1");
            //端口号
            JedisConnectionFactory.setPort(6379);
            return JedisConnectionFactory;
        }
        
        @Bean
        public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
            RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
            template.setConnectionFactory(factory);//关联
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new StringRedisSerializer());
            return template;
        }
        
    }
    
    

    通过注解@ConfigurationProperties(prefix=""),提取完信息如下:

    package com.test.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    public class RedisConfig2 {
    
        @Bean
        @ConfigurationProperties(prefix="spring.redis.pool")
        public JedisPoolConfig jedisPoolConfig() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            return jedisPoolConfig;
        }
        
        @Bean
        @ConfigurationProperties(prefix="spring.redis")
        public JedisConnectionFactory jedisPoolConfig(JedisPoolConfig config) {
            JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory();
            //关联连接池的配置对象
            JedisConnectionFactory.setPoolConfig(config);
            return JedisConnectionFactory;
        }
        
        @Bean
        public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
            RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
            template.setConnectionFactory(factory);//关联
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new StringRedisSerializer());
            return template;
        }
        
    }
    
    

    application.properties如下:

    spring.redis.pool.max-idle=10
    spring.redis.pool.min-idle=5
    spring.redis.pool.max-total=30
    
    spring.redis.hostName=127.0.0.1
    spring.redis.port=6379
    

    相关文章

      网友评论

          本文标题:springboot自学笔记-整合redis(二)

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