package com.hx.redisson.register; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import com.hx.redisson.entity.RedissondbConfigEntity; import com.hx.redisson.manager.RedisManager; import org.apache.commons.collections.CollectionUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.client.codec.StringCodec; import org.redisson.config.Config; import org.redisson.config.SingleServerConfig; import org.redisson.spring.data.connection.RedissonConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotationMetadata; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.List; /** *
* 作用: *
* 读取系统配置,系统启动时,读取redis 的配置,初始化所有的redistemplate
* 并动态注册为bean
*
* @author gengzi
* @date 2021年1月5日22:16:29
*/
@Configuration
public class RedisRegister implements EnvironmentAware, ImportBeanDefinitionRegistrar {
private Logger logger = LoggerFactory.getLogger(RedisRegister.class);
/**用于获取环境配置*/
private Environment environment;
/**用于绑定对象*/
private Binder binder;
/**
* 设置环境
*
* @param environment
*/
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
this.binder = Binder.get(environment);
}
/**
* 注册bean
*
* @param importingClassMetadata
* @param registry
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
logger.info("《《《动态注册bean开始》》》");
RedissondbConfigEntity redissondb;
try {
redissondb = binder.bind("redissondb", RedissondbConfigEntity.class).get();
} catch (Exception e) {
logger.error("读取redissondb环境配置失败", e);
return;
}
List