RedisProperties。和@Value(“ $ {spring.redis.pool.max-idle:100}”)

时间:2019-01-17 12:26:36

标签: spring jedis

嗨,我想知道springboot如何处理属性文件。

我没有尝试

@配置 公共类RedisConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);

@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.pool.max-total:100}")
private int poolMaxTotal;
@Value("${spring.redis.pool.max-idle:100}")
private int poolMaxIdle;
@Value("${spring.redis.pool.min-idle:10}")
private int poolMinIdle;
@Value("${spring.redis.pool.max-wait:2000}")
private int poolMaxWaitMillis;

public @Bean RedisTemplate redisTemplateForUser() {
    RedisTemplate redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory());
    redisTemplate.setKeySerializer(stringRedisSerializer());
    redisTemplate.setValueSerializer(redisJsonSerializer());
    return redisTemplate;
}

public @Bean StringRedisSerializer stringRedisSerializer(){
    return new StringRedisSerializer();
}

public @Bean Jackson2JsonRedisSerializer redisJsonSerializer(){
    return new Jackson2JsonRedisSerializer<>(UserFromCache.class);
}

public @Bean RedisTemplate redisTemplate() {
    RedisTemplate redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory());
    return redisTemplate;
}

public @Bean RedisConnectionFactory connectionFactory() {
    JedisPoolConfig pool = new JedisPoolConfig();
    pool.setMaxTotal(poolMaxTotal);
    pool.setMaxIdle(poolMaxIdle);
    pool.setMinIdle(poolMinIdle);
    pool.setMaxWaitMillis(poolMaxWaitMillis);
    pool.setTestOnBorrow(true);
    JedisConnectionFactory jcf = new JedisConnectionFactory(pool);
    jcf.setHostName(host);

通常我们使用属性文件中的值来配置池,我假设Spring可以通过此注释从属性中读取值(我没有检查此注释的作用)。我们为什么要开设一个班级,这是什么故事:

    package org.springframework.boot.autoconfigure.data.redis;

    import java.util.List;
    import org.springframework.boot.context.properties.ConfigurationProperties;

    @ConfigurationProperties(
        prefix = "spring.redis"
    )
    public class RedisProperties {
        private int database = 0;
        private String url;
        private String host = "localhost";
        private String password;
        private int port = 6379;

0 个答案:

没有答案