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