Fix problem when update fizz config dynamically

This commit is contained in:
hongqiaowei
2023-02-10 18:07:19 +08:00
parent ed7863f57b
commit 84f087e1c1
2 changed files with 21 additions and 3 deletions

View File

@@ -17,6 +17,7 @@
package com.fizzgate.beans.factory.config; package com.fizzgate.beans.factory.config;
import com.fizzgate.config.FizzConfigConfiguration;
import com.fizzgate.context.config.annotation.FizzRefreshScope; import com.fizzgate.context.config.annotation.FizzRefreshScope;
import com.fizzgate.util.Consts; import com.fizzgate.util.Consts;
import com.fizzgate.util.JacksonUtils; import com.fizzgate.util.JacksonUtils;
@@ -35,6 +36,8 @@ import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate; import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import java.util.HashMap; import java.util.HashMap;
@@ -69,6 +72,11 @@ public class FizzBeanFactoryPostProcessor implements BeanFactoryPostProcessor, E
if (fizzConfigEnable.equals(Consts.S.TRUE)) { if (fizzConfigEnable.equals(Consts.S.TRUE)) {
// initReactiveStringRedisTemplate(); // initReactiveStringRedisTemplate();
// initFizzPropertySource(); // initFizzPropertySource();
Map<String, Object> sources = FizzEnvironmentPostProcessor.getSources();
MapPropertySource fizzPropertySource = new MapPropertySource(FizzConfigConfiguration.PROPERTY_SOURCE + "AfterBeanFactory", sources);
environment.getPropertySources().addFirst(fizzPropertySource);
initBeanProperty2beanMap(beanFactory); initBeanProperty2beanMap(beanFactory);
} }
} }

View File

@@ -18,6 +18,7 @@ import org.springframework.boot.logging.DeferredLog;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener; import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
@@ -34,18 +35,28 @@ import java.util.Map;
* @author hongqiaowei * @author hongqiaowei
*/ */
public class FizzEnvironmentPostProcessor implements EnvironmentPostProcessor, SmartApplicationListener { public class FizzEnvironmentPostProcessor implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
private static final DeferredLog LOGGER = new DeferredLog(); private static final DeferredLog LOGGER = new DeferredLog();
private static Logger LOG = null; private static Logger LOG = null;
private static final Map<String, Object> sources = new HashMap<>();
private ConfigurableEnvironment environment; private ConfigurableEnvironment environment;
private ReactiveStringRedisTemplate reactiveStringRedisTemplate; private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
protected static Map<String, Object> getSources() {
return sources;
}
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
@Override @Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String fizzConfigEnable = environment.getProperty("fizz.config.enable", Consts.S.TRUE); String fizzConfigEnable = environment.getProperty("fizz.config.enable", Consts.S.TRUE);
@@ -84,8 +95,7 @@ public class FizzEnvironmentPostProcessor implements EnvironmentPostProcessor, S
private void initFizzPropertySource() { private void initFizzPropertySource() {
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> sources = new HashMap<>(); MapPropertySource fizzPropertySource = new MapPropertySource(FizzConfigConfiguration.PROPERTY_SOURCE + "AfterEnv", sources);
MapPropertySource fizzPropertySource = new MapPropertySource(FizzConfigConfiguration.PROPERTY_SOURCE, sources);
propertySources.addFirst(fizzPropertySource); propertySources.addFirst(fizzPropertySource);
Result<?> result = Result.succ(); Result<?> result = Result.succ();