guang
2024-06-14 93ac75952d5d6f64256434b99c8da612fa339263
提交 | 用户 | age
da6731 1 package com.hx.redisson.config;
C 2
3 import com.hx.redisson.entity.RedissondbConfigEntity;
4 import com.hx.redisson.manager.RedisManager;
5 import org.apache.commons.collections.CollectionUtils;
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 /**实现 EnvironmentAware 用于获取全局环境
34 实现 ApplicationContextAware 用于获取Spring Context 上下文*/
35 public class RedisBeanInit implements EnvironmentAware, ApplicationContextAware {
36     private Logger logger = LoggerFactory.getLogger(RedisBeanInit.class);
37
38      /**用于获取环境配置*/
39     private Environment environment;
40     /**用于绑定对象*/
41     private Binder binder;
42     /**Spring context*/
43     private ApplicationContext applicationContext;
44     /**线程安全的hashmap*/
45     private Map<String, RedisTemplate> redisTemplateMap = new ConcurrentHashMap<>();
46
47     @Override
48     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
49         this.applicationContext = applicationContext;
50     }
51     /**
52      * 设置环境
53      *
54      * @param environment
55      */
56     @Override
57     public void setEnvironment(Environment environment) {
58         this.environment = environment;
59         this.binder = Binder.get(environment);
60     }
61
62
63     @PostConstruct // Constructor >> @Autowired >> @PostConstruct 用于执行一个 非静态的void 方法,常应用于初始化资源
64     public void initAllRedisTemlate() {
65         logger.info("<<<初始化系统的RedisTemlate开始>>>");
66         RedissondbConfigEntity redissondb;
67         try {
68             redissondb = binder.bind("redissondb", RedissondbConfigEntity.class).get();
69         } catch (Exception e) {
70             logger.error("读取redissondb环境配置失败", e);
71             return;
72         }
73         List<Integer> databases = redissondb.getDatabases();
74         if (CollectionUtils.isNotEmpty(databases)) {
75             databases.forEach(db -> {
76                 Object bean = applicationContext.getBean("redisTemplate" + db);
77                 if (bean != null && bean instanceof RedisTemplate) {
78                     redisTemplateMap.put("redisTemplate" + db, (RedisTemplate) bean);
79                 } else {
80                     throw new RuntimeException("初始化RedisTemplate" + db + "失败,请检查配置");
81                 }
82             });
83         }
84         logger.info("已经装配的redistempleate,map:{}", redisTemplateMap);
85         logger.info("<<<初始化系统的RedisTemlate完毕>>>");
86     }
87
88     @Bean
89     public RedisManager getRedisManager() {
90         return new RedisManager(redisTemplateMap);
91     }
92 }