Spring boot 手动配置redis

来源:互联网 发布:qq远程监控软件 编辑:程序博客网 时间:2024/06/02 04:20


MyRedisProperties.java,读取redis配置

package com.redis.config;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties( prefix = "my.redis")public class MyRedisProperties { private int database = 0; private String host = "localhost"; private String password; private int port = 6379; private MyRedisProperties.Pool pool; private MyRedisProperties.Sentinel sentinel; public MyRedisProperties() { } public String getHost() {     return this.host; } public void setHost(String host) {     this.host = host; } public int getPort() {     return this.port; } public void setPort(int port) {     this.port = port; } public String getPassword() {     return this.password; } public void setPassword(String password) {     this.password = password; } public MyRedisProperties.Pool getPool() {     return this.pool; } public void setPool(MyRedisProperties.Pool pool) {     this.pool = pool; } public int getDatabase() {     return this.database; } public void setDatabase(int database) {     this.database = database; } public MyRedisProperties.Sentinel getSentinel() {     return this.sentinel; } public void setSentinel(MyRedisProperties.Sentinel sentinel) {     this.sentinel = sentinel; } public static class Sentinel {     private String master;     private String nodes;     public Sentinel() {     }     public String getMaster() {         return this.master;     }     public void setMaster(String master) {         this.master = master;     }     public String getNodes() {         return this.nodes;     }     public void setNodes(String nodes) {         this.nodes = nodes;     } } public static class Pool {     private int maxIdle = 8;     private int minIdle = 0;     private int maxActive = 8;     private int maxWait = -1;     public Pool() {     }     public int getMaxIdle() {         return this.maxIdle;     }     public void setMaxIdle(int maxIdle) {         this.maxIdle = maxIdle;     }     public int getMinIdle() {         return this.minIdle;     }     public void setMinIdle(int minIdle) {         this.minIdle = minIdle;     }     public int getMaxActive() {         return this.maxActive;     }     public void setMaxActive(int maxActive) {         this.maxActive = maxActive;     }     public int getMaxWait() {         return this.maxWait;     }     public void setMaxWait(int maxWait) {         this.maxWait = maxWait;     } }}



RedisConfig.java,  手动配置redis连接池

package com.redis.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;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.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import redis.clients.jedis.JedisPoolConfig;import org.springframework.context.annotation.Bean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;/** * Created by zhangzw on 2016/1/8. */@Configurationpublic class RedisConfig {@Bean(name = {"MyRedisProperties"})    @ConditionalOnMissingBean    public MyRedisProperties redisProperties() {        return new MyRedisProperties();    }@Autowired@Qualifier("MyRedisProperties")    private MyRedisProperties properties;    @Bean    public RedisConnectionFactory jedisConnectionFactory(){        JedisPoolConfig poolConfig = new JedisPoolConfig();        poolConfig.setMaxTotal(properties.getPool().getMaxActive());        poolConfig.setMaxIdle(properties.getPool().getMaxIdle());        poolConfig.setMaxWaitMillis(properties.getPool().getMaxWait());        poolConfig.setTestOnBorrow(true);        poolConfig.setTestOnCreate(true);        poolConfig.setTestWhileIdle(true);        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);        jedisConnectionFactory.setHostName(properties.getHost());        if(null != properties.getPassword()){        jedisConnectionFactory.setPassword(properties.getPassword());        }        jedisConnectionFactory.setPort(properties.getPort());                //其他配置,可再次扩展                return jedisConnectionFactory;    }    @Bean(name="myRedisTemplate")    @ConditionalOnMissingBean(            name = {"myRedisTemplate"}    )    public RedisTemplate redisTemplate(){        RedisTemplate redisTemplate = new RedisTemplate();        redisTemplate.setConnectionFactory(jedisConnectionFactory());        return redisTemplate;    }}


AppTest.java 测试用例

import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.dubbo.test.api.Application;@RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = Application.class)public class AppTest {@Autowired@Qualifier("myRedisTemplate")private RedisTemplate myRedisTemplate;@Test    public void test() throws Exception {String key = "key1";String value = "value1";set(key, value);System.out.println(get(key));    }public  String get(String key){        return myRedisTemplate.opsForValue().get(key).toString();    }public  void set(String key, String value){myRedisTemplate.opsForValue().set(key, value);}}


0 1
原创粉丝点击