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
- Spring boot 手动配置redis
- spring-boot redis 配置
- spring-boot redis配置
- spring boot redis配置
- spring-boot配置redis cluster
- Spring Boot Redis 集成配置
- spring boot 注解配置redis
- spring boot 集成redis 配置
- Spring-boot通过redisTemplate使用redis(无须手动序列化)
- spring-boot-data-redis线程池配置
- spring-boot-starter-redis配置详解
- spring boot 笔记(三):配置redis
- Spring Boot中redis的配置实例
- spring boot下redis的配置使用
- Spring Boot集成Redis java实现配置
- Spring Boot集成Redis实现自动配置
- spring Boot redis连接池配置,Spring Session配置
- spring Boot redis连接池配置,Spring Session配置
- 利用JS 在网页上获取并显示当前日期 星期
- oracle创建一个函数例子
- codevs 2471 表达式的转换--二叉树
- MFC入门教程
- VS2010 MFC中实现printf调试功能,即MFC程序利用控制台输出调试信息
- Spring boot 手动配置redis
- sparksql语法,通过编程方式读txt
- SQL Server 管理数据收集
- Java中的继承
- 如何生成不规则形状的mask,以解决对图像不规则区域设置ROI的问题
- iOS调试技巧
- C/C++内存分配
- Material design
- 2016.1.8 个人总结