【SpringBoot 基础系列】实现一个自定义配置加载器(应用篇) (2)

配置容器,保存配置与 field 映射关系,提供配置的基本操作

@Slf4j public class MetaContainer { private MetaValHolder metaValHolder; // 保存配置与Field之间的绑定关系 private Map<String, Set<InvokeCell>> metaCache = new ConcurrentHashMap<>(); public MetaContainer(MetaValHolder metaValHolder) { this.metaValHolder = metaValHolder; } public String getProperty(String key) { return metaValHolder.getProperty(key); } // 用于新增绑定关系并初始化 public void addInvokeCell(MetaVal metaVal, Object target, Field field) throws IllegalAccessException { String metaKey = metaVal.value(); if (!metaCache.containsKey(metaKey)) { synchronized (this) { if (!metaCache.containsKey(metaKey)) { metaCache.put(metaKey, new HashSet<>()); } } } metaCache.get(metaKey).add(new InvokeCell(metaVal, target, field, getProperty(metaKey))); } // 配置更新 public void updateMetaVal(String metaKey, String oldVal, String newVal) { Set<InvokeCell> cacheSet = metaCache.get(metaKey); if (CollectionUtils.isEmpty(cacheSet)) { return; } cacheSet.forEach(s -> { try { s.update(newVal); log.info("update {} from {} to {}", s.getSignature(), oldVal, newVal); } catch (IllegalAccessException e) { e.printStackTrace(); } }); } @Data public static class InvokeCell { private MetaVal metaVal; private Object target; private Field field; private String signature; private Object value; public InvokeCell(MetaVal metaVal, Object target, Field field, String value) throws IllegalAccessException { this.metaVal = metaVal; this.target = target; this.field = field; field.setAccessible(true); signature = target.getClass().getName() + "." + field.getName(); this.update(value); } public void update(String value) throws IllegalAccessException { this.value = this.metaVal.parser().parse(value); field.set(target, this.value); } } } 5. Event/Listener

接下来就是事件通知机制的支持了

MetaChangeEvent 配置变更事件,提供基本的三个信息,配置 key,原 value,新 value

@ToString @EqualsAndHashCode public class MetaChangeEvent extends ApplicationEvent { private static final long serialVersionUID = -9100039605582210577L; private String key; private String oldVal; private String newVal; /** * Create a new {@code ApplicationEvent}. * * @param source the object on which the event initially occurred or with * which the event is associated (never {@code null}) */ public MetaChangeEvent(Object source) { super(source); } public MetaChangeEvent(Object source, String key, String oldVal, String newVal) { super(source); this.key = key; this.oldVal = oldVal; this.newVal = newVal; } public String getKey() { return key; } public String getOldVal() { return oldVal; } public String getNewVal() { return newVal; } }

MetaChangeListener 事件处理器,刷新@MetaVal 绑定的配置

public class MetaChangeListener implements ApplicationListener<MetaChangeEvent> { private MetaContainer metaContainer; public MetaChangeListener(MetaContainer metaContainer) { this.metaContainer = metaContainer; } @Override public void onApplicationEvent(MetaChangeEvent event) { metaContainer.updateMetaVal(event.getKey(), event.getOldVal(), event.getNewVal()); } } 6. bean 配置

上面五步,一个自定义的配置加载器基本上就完成了,剩下的就是 bean 的声明

@Configuration public class DynamicConfig { @Bean @ConditionalOnMissingBean(MetaValHolder.class) public MetaValHolder metaValHolder() { return key -> null; } @Bean public MetaContainer metaContainer(MetaValHolder metaValHolder) { return new MetaContainer(metaValHolder); } @Bean public MetaValueRegister metaValueRegister(MetaContainer metaContainer) { return new MetaValueRegister(metaContainer); } @Bean public MetaChangeListener metaChangeListener(MetaContainer metaContainer) { return new MetaChangeListener(metaContainer); } }

以二方工具包方式提供外部使用,所以需要在资源目录下,新建文件META-INF/spring.factories(常规套路了)

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.git.hui.boot.dynamic.config.DynamicConfig 6. 测试

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyypzw.html