代码如下:
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.password}")
private String password;
@Value("${identify.redis.environment}")
private int redisEnvironment;
@Value("${spring.redis.cluster.nodes}")
private String nodes;
@Value("${spring.redis.cluster.max-redirects}")
private String maxRedirects;
/**
* 获取redis连接池配置
* @return
*/
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxTotal(maxActive);
return jedisPoolConfig;
}
/**
* redis集群配置
* @return
*/
@Bean
public RedisClusterConfiguration redisClusterConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
String[] serverArray = nodes.split(",");
Set<RedisNode> nodes = new HashSet<RedisNode>();
if(serverArray.length > 1) {
for(String ipPort:serverArray){
String[] ipAndPort = ipPort.split(":");
nodes.add(new RedisNode(ipAndPort[0].trim(),Integer.valueOf(ipAndPort[1])));
}
redisClusterConfiguration.setClusterNodes(nodes);
redisClusterConfiguration.setMaxRedirects(Integer.valueOf(maxRedirects).intValue());
}
return redisClusterConfiguration;
}
/**
* redis连接工厂类,此类用于获取从池中获取redis连接
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnectionFactory = null;
//此处区分环境为单机redis与集群redis,不同的环境加载不同的jedisConnectionFactory工厂
if(redisEnvironment == 0) {
//单机形式
jedisConnectionFactory = new JedisConnectionFactory(getJedisPoolConfig());
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(Integer.valueOf(port).intValue());
jedisConnectionFactory.setPassword(password);
}else {
//集群形式
jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration(),getJedisPoolConfig());
jedisConnectionFactory.setPassword(password);
}
//集群单机共同配置
jedisConnectionFactory.setDatabase(database);
jedisConnectionFactory.setTimeout(timeout);
jedisConnectionFactory.setUsePool(true);
return jedisConnectionFactory;
}
/**
* 初始化redisTemplate模板
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
initDomainRedisTemplate(redisTemplate);
return redisTemplate;
}
/**
* 设置数据存入 redis 的序列化方式,并开启事务
*
*/
private void initDomainRedisTemplate(RedisTemplate<Object, Object> redisTemplate) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setEnableTransactionSupport(false);
redisTemplate.setConnectionFactory(jedisConnectionFactory());
}
上述代码向容器重新声明了模板类,利用@Configuration进行配置,我在service层 利用@Autowired注入RedisTemplate时,系统不能正常启动,如果利用@Resource注入的话是可以的。
上述的前提我是在启动类中排除了redis自动配置的类,RedisAutoConfiguration.class和RedisRepositoriesAutoConfiguration.class。有没有经历过这个坑的大神给我详解一下谢谢
https://stackoverflow.com/questions/4093504/resource-vs-autowired
标注本身的差别不是太大,上面这里有解释
这个问题打开DEBUG日志,看下哪里出错了
谢谢您的回复,此处我已经了解了