美文网首页
spring boot redis配置详解

spring boot redis配置详解

作者: java修炼 | 来源:发表于2018-08-15 17:18 被阅读0次

直接上代码

package cc.openwiki.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@Configuration
public class RedisConfiguration {
    private RelaxedPropertyResolver propertyResolver;

    @Autowired
    public void setEnvironment(Environment env) {
        this.propertyResolver = new RelaxedPropertyResolver(env, "spring.redis.");
    }

    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPort(Integer.valueOf(propertyResolver.getProperty("port")));
        factory.setHostName(propertyResolver.getProperty("host"));
        factory.setTimeout(Integer.valueOf(propertyResolver.getProperty("timeout")));

        return factory;
    }

    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
        return stringRedisTemplate;
    }

    @Bean
    public HashOperations<String, String, String> hashOperations(StringRedisTemplate redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations<String, String> valueOperations(StringRedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

}

相关文章

网友评论

      本文标题:spring boot redis配置详解

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