网上找到的配置方式都比较繁杂,要一个个字段set到redis里,这边提供一个非常简便的配置方式。


一、引入Maven

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>


二、application.properties中的配置

spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.encode=utf-8
spring.redis.pool.maxIdle=300
spring.redis.pool.maxTotal=600
spring.redis.pool.timeBetweenEvictionRunsMillis=30000
spring.redis.pool.minEvictableIdleTimeMillis=30000
spring.redis.pool.testOnBorrow=true


三、新增RedisConfig.java配置文件

使用@ConfigurationProperties(prefix="spring.redis")注解,会自动将字段注入到bean中

package cn.reinforce.cloud.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 幻幻Fate
 * @create 2017/10/2
 * @since
 */
@Configuration
@EnableCaching
public class RedisConfig{

    private static Logger logger = Logger.getLogger(RedisConfig.class);

    @Bean
    @ConfigurationProperties(prefix="spring.redis.pool")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix="spring.redis")
    public JedisConnectionFactory getConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        logger.info("JedisConnectionFactory bean init success.");
        return factory;
    }

    @Bean
    @Primary
    public RedisTemplate redisTemplate() {
        JedisConnectionFactory jedisConnectionFactory = getConnectionFactory();
        StringRedisTemplate template = new StringRedisTemplate(jedisConnectionFactory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}


四、调用Redis

在其他要使用redis的类中,加入

@Autowired
protected RedisTemplate redisTemplate;

即可正常使用redis了


项目参考:https://gitee.com/hhfate/fate-spring-boot-basic(注:因为没有sql文件,项目暂时是跑不起来的)


--------------------------------------------------------2018-7-2--------------------------------------------------------------

结合Spring Session Starter,整个redis的配置也简化了,详见以上项目的最新版

赞助本站,网站的持续发展离不开你们的支持!一分也是爱ヾ(◍°∇°◍)ノ゙
 本文链接: ,花了好多脑细胞写的,转载请注明链接喔~~
登陆
      正在加载评论
 书籍推荐