org.zodiac.autoconfigure.redis.RedisApplicationContextInitializer Maven / Gradle / Ivy
package org.zodiac.autoconfigure.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.redis.cachecloud.RedisCacheCloudInfo;
import org.zodiac.redis.jedis.JedisPostProcessor;
import org.zodiac.redis.redisson.RedissonPostProcessor;
import java.io.IOException;
import java.util.List;
public class RedisApplicationContextInitializer
implements ApplicationContextInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisApplicationContextInitializer.class);
private static final String JEDIS_PATH = "redis.clients.jedis.Jedis";
private static final String REDISSON_PATH = "org.redisson.api.RedissonClient";
private boolean checkRedissonLockProperties(final RedissonListProperties redissonProperties) {
if (redissonProperties != null && redissonProperties.getSource().size() != 0) {
return true;
}
return false;
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
RedissonListProperties redissonProperties = loadRedissonProperties(applicationContext);
RedisCacheCloudListProperties jedisProperties = loadJedisProperties(applicationContext);
Environment env = applicationContext.getEnvironment();
RedisCacheCloudProperties redisProperties = loadRedisProperties(applicationContext);
if (redisProperties != null) {
jedisProperties = jedisProperties == null ? new RedisCacheCloudListProperties() : jedisProperties;
List old = jedisProperties.getSource();
jedisProperties.getSource().add(redisProperties);
if (old != null) {
jedisProperties.getSource().addAll(old);
}
}
if (ClassUtils.isPresent(JEDIS_PATH, RedisApplicationContextInitializer.class.getClassLoader())) {
boolean redissonLockExit = checkRedissonLockProperties(redissonProperties);
new JedisPostProcessor(jedisProperties, !redissonLockExit, env)
.process(applicationContext.getBeanFactory());
}
if (ClassUtils.isPresent(REDISSON_PATH, RedisApplicationContextInitializer.class.getClassLoader())) {
try {
new RedissonPostProcessor(redissonProperties, env).process(applicationContext.getBeanFactory());
} catch (IOException e) {
LOGGER.error("Initial Redisson error, e:{}", e);
throw new RuntimeException(e);
}
}
}
private RedisCacheCloudProperties loadRedisProperties(ConfigurableApplicationContext applicationContext) {
RedisCacheCloudProperties properties = Springs.bindAndGetProperties(applicationContext.getEnvironment(),
SystemPropertiesConstants.Spring.SPRING_REDIS_PREFIX, RedisCacheCloudProperties.class);
return properties;
}
private RedisCacheCloudListProperties loadJedisProperties(ConfigurableApplicationContext applicationContext) {
RedisCacheCloudListProperties properties = Springs.bindAndGetProperties(applicationContext.getEnvironment(),
SystemPropertiesConstants.Zodiac.SPRING_REDIS_CACHE_CLOUD_JEDIS_PREFIX,
RedisCacheCloudListProperties.class);
return properties;
}
private RedissonListProperties loadRedissonProperties(ConfigurableApplicationContext applicationContext) {
RedissonListProperties properties = Springs.bindAndGetProperties(applicationContext.getEnvironment(),
SystemPropertiesConstants.Zodiac.SPRING_REDIS_CACHE_CLOUD_REDISSON_PREFIX, RedissonListProperties.class);
return properties;
}
}