chenjiahe
2022-06-28 4484acb56810d06ef5c2f78190a93688a61f83a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.hx.redisson.templates.config;
 
import com.hx.redisson.templates.entity.RedissondbConfigEntity;
import com.hx.redisson.templates.manager.RedisManager;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
 
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * RedisTemplate 初始化类
 *
 * @author CJH
 * @date 2020年12月16日22:38:46
 */
/**要在RedisAutoConfiguration 自动配置前执行*/
@AutoConfigureBefore({RedisAutoConfiguration.class})
@Configuration
/**实现 EnvironmentAware 用于获取全局环境
实现 ApplicationContextAware 用于获取Spring Context 上下文*/
public class RedisBeanInit implements EnvironmentAware, ApplicationContextAware {
    private Logger logger = LoggerFactory.getLogger(RedisBeanInit.class);
 
     /**用于获取环境配置*/
    private Environment environment;
    /**用于绑定对象*/
    private Binder binder;
    /**Spring context*/
    private ApplicationContext applicationContext;
    /**线程安全的hashmap*/
    private Map<String, RedisTemplate> redisTemplateMap = new ConcurrentHashMap<>();
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    /**
     * 设置环境
     *
     * @param environment
     */
    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
        this.binder = Binder.get(environment);
    }
 
 
    @PostConstruct // Constructor >> @Autowired >> @PostConstruct 用于执行一个 非静态的void 方法,常应用于初始化资源
    public void initAllRedisTemlate() {
        logger.info("<<<初始化系统的RedisTemlate开始>>>");
        RedissondbConfigEntity redissondb;
        try {
            redissondb = binder.bind("redissondb", RedissondbConfigEntity.class).get();
        } catch (Exception e) {
            logger.error("读取redissondb环境配置失败", e);
            return;
        }
        List<Integer> databases = redissondb.getDatabases();
        if (CollectionUtils.isNotEmpty(databases)) {
            databases.forEach(db -> {
                Object bean = applicationContext.getBean("redisTemplate" + db);
                if (bean != null && bean instanceof RedisTemplate) {
                    redisTemplateMap.put("redisTemplate" + db, (RedisTemplate) bean);
                } else {
                    throw new RuntimeException("初始化RedisTemplate" + db + "失败,请检查配置");
                }
            });
        }
        logger.info("已经装配的redistempleate,map:{}", redisTemplateMap);
        logger.info("<<<初始化系统的RedisTemlate完毕>>>");
    }
 
    @Bean
    public RedisManager getRedisManager() {
        return new RedisManager(redisTemplateMap);
    }
}