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