一.属性类
package com.djhu.followup.config;
/**
- Created by zw on 2017/12/13.
*/
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = “spring.boot.djhu.redis”)
public class RedisProperties {
public static final int DEFAULT_MAXIDLE = 30;
public static final int DEFAULT_MAXACTIVE = 30;
public static final boolean DEFAULT_TESTONBORROW = true;
private String ip;
private Integer port;
private String auth;
private Integer maxIdle = DEFAULT_MAXIDLE;
private Integer maxActive = DEFAULT_MAXACTIVE;
private Boolean testOnBorrow = DEFAULT_TESTONBORROW;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Boolean getTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(Boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
二.配置类
package com.djhu.followup.config;
/**
- Created by zw on 2017/12/13.
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
/**
- code is far away from bug with the animal protecting
- ┏┓ ┏┓
- ┏┛┻━━━┛┻┓
- ┃ ┃
- ┃ ━ ┃
- ┃ ┳┛ ┗┳ ┃
- ┃ ┃
- ┃ ┻ ┃
- ┃ ┃
- ┗━┓ ┏━┛
- ┃ ┃神兽保佑
- ┃ ┃代码无BUG!
- ┃ ┗━━━┓
- ┃ ┣┓
- ┃ ┏┛
- ┗┓┓┏━┳┓┏┛
- ┃┫┫ ┃┫┫
- ┗┻┛ ┗┻┛
-
-
*/
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(value = {Jedis.class, RedisTemplate.class})
@ConditionalOnProperty(prefix = “spring.boot.djhu.redis”, value=“enabled”, matchIfMissing =false )
public class RedisAutoConfiguration {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setHostName(redisProperties.getIp());
jedisConnectionFactory.setPassword(redisProperties.getAuth());
jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
jedisConnectionFactory.afterPropertiesSet();
return jedisConnectionFactory;
}
@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
//@ConditionalOnClass()
}