feature:add @ConditionalOnConfigReflectEnabled annotation

pull/496/head
wulingxiao 3 years ago
parent 3377a7e775
commit 72b76af11f

@ -18,10 +18,10 @@
package com.tencent.cloud.polaris.config; package com.tencent.cloud.polaris.config;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor; import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
import com.tencent.cloud.polaris.config.condition.ConditionalOnConfigReflectEnabled; import com.tencent.cloud.polaris.config.condition.ConditionalOnConfigReflectEnabled;
@ -33,6 +33,7 @@ import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SearchStrategy;
@ -63,46 +64,47 @@ public class PolarisConfigAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
@ConditionalOnNonDefaultBehavior
public ConfigurationPropertiesRebinder smartConfigurationPropertiesRebinder(
ConfigurationPropertiesBeans beans) {
// If using default behavior, not use SmartConfigurationPropertiesRebinder.
// Minimize te possibility of making mistakes.
return new SmartConfigurationPropertiesRebinder(beans);
}
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) {
return new PolarisRefreshContextConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher);
}
@ConditionalOnConfigReflectEnabled @ConditionalOnConfigReflectEnabled
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(PolarisConfigAutoConfiguration.class)
public static class PolarisReflectRefresherAutoConfiguration {
@Bean
public SpringValueRegistry springValueRegistry() { public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry(); return new SpringValueRegistry();
} }
@Bean @Bean
@ConditionalOnConfigReflectEnabled
public PlaceholderHelper placeholderHelper() { public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper(); return new PlaceholderHelper();
} }
@Bean @Bean
@ConditionalOnConfigReflectEnabled public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper,
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper, SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) { SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties); return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
} }
@Bean @Bean
@ConditionalOnConfigReflectEnabled public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
public PolarisPropertySourceRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
PolarisPropertySourceManager polarisPropertySourceManager,
SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) { PlaceholderHelper placeholderHelper) {
return new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper); return new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper);
}
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
@ConditionalOnNonDefaultBehavior
public ConfigurationPropertiesRebinder smartConfigurationPropertiesRebinder(
ConfigurationPropertiesBeans beans) {
// If using default behavior, not use SmartConfigurationPropertiesRebinder.
// Minimize te possibility of making mistakes.
return new SmartConfigurationPropertiesRebinder(beans);
} }
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisPropertySourceRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) {
return new PolarisRefreshContextPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher);
} }
} }

@ -38,10 +38,10 @@ import org.springframework.util.CollectionUtils;
* *
* @author lepdou 2022-03-28 * @author lepdou 2022-03-28
*/ */
public abstract class PolarisPropertySourceAutoRefresher public abstract class PolarisConfigPropertyAutoRefresher
implements ApplicationListener<ApplicationReadyEvent>, PolarisPropertySourceRefresher { implements ApplicationListener<ApplicationReadyEvent>, PolarisConfigPropertyRefresher {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisPropertySourceAutoRefresher.class); private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigPropertyAutoRefresher.class);
private final PolarisConfigProperties polarisConfigProperties; private final PolarisConfigProperties polarisConfigProperties;
@ -49,7 +49,7 @@ public abstract class PolarisPropertySourceAutoRefresher
private final AtomicBoolean registered = new AtomicBoolean(false); private final AtomicBoolean registered = new AtomicBoolean(false);
public PolarisPropertySourceAutoRefresher( public PolarisConfigPropertyAutoRefresher(
PolarisConfigProperties polarisConfigProperties, PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager) { PolarisPropertySourceManager polarisPropertySourceManager) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;

@ -21,12 +21,12 @@ package com.tencent.cloud.polaris.config.adapter;
import java.util.Set; import java.util.Set;
/** /**
* PolarisPropertySourceRefresher refresh spring value filed and configurationProperties bean * PolarisConfigPropertyRefresher refresh spring value filed and configurationProperties bean
* when config exchange. * when config exchange.
* *
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
public interface PolarisPropertySourceRefresher { public interface PolarisConfigPropertyRefresher {
/** /**
* refresh the attribute with @Value annotation. * refresh the attribute with @Value annotation.

@ -38,15 +38,15 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
/** /**
* PolarisReflectPropertySourceAutoRefresher to refresh config in reflect type * PolarisReflectConfigPropertyAutoRefresher to refresh config in reflect type
* we can use it by setting spring.cloud.polaris.config.refresh-type=reflect. * we can use it by setting spring.cloud.polaris.config.refresh-type=reflect.
* *
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
public class PolarisReflectPropertySourceAutoRefresher extends PolarisPropertySourceAutoRefresher public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigPropertyAutoRefresher
implements ApplicationContextAware { implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectPropertySourceAutoRefresher.class); private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectConfigPropertyAutoRefresher.class);
private final SpringValueRegistry springValueRegistry; private final SpringValueRegistry springValueRegistry;
@ -58,7 +58,7 @@ public class PolarisReflectPropertySourceAutoRefresher extends PolarisPropertySo
private TypeConverter typeConverter; private TypeConverter typeConverter;
public PolarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisReflectConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) { PlaceholderHelper placeholderHelper) {
super(polarisConfigProperties, polarisPropertySourceManager); super(polarisConfigProperties, polarisPropertySourceManager);

@ -25,15 +25,15 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.cloud.context.refresh.ContextRefresher;
/** /**
* PolarisRefreshContextPropertySourceAutoRefresher refresh config by refreshContext. * PolarisRefreshContextConfigPropertyAutoRefresher refresh config by refreshContext.
* *
* @author lingxiao.wlx * @author lingxiao.wlx
*/ */
public class PolarisRefreshContextPropertySourceAutoRefresher extends PolarisPropertySourceAutoRefresher { public class PolarisRefreshContextConfigPropertyAutoRefresher extends PolarisConfigPropertyAutoRefresher {
private final ContextRefresher contextRefresher; private final ContextRefresher contextRefresher;
public PolarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, public PolarisRefreshContextConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, PolarisPropertySourceManager polarisPropertySourceManager,
ContextRefresher contextRefresher) { ContextRefresher contextRefresher) {
super(polarisConfigProperties, polarisPropertySourceManager); super(polarisConfigProperties, polarisPropertySourceManager);

@ -47,7 +47,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* test for {@link PolarisReflectPropertySourceAutoRefresher}. * test for {@link PolarisReflectConfigPropertyAutoRefresher}.
* *
* @author lepdou 2022-06-11 * @author lepdou 2022-06-11
*/ */
@ -70,7 +70,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Test @Test
public void testConfigFileChanged() throws Exception { public void testConfigFileChanged() throws Exception {
PolarisReflectPropertySourceAutoRefresher refresher = new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties, PolarisReflectConfigPropertyAutoRefresher refresher = new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties,
polarisPropertySourceManager, springValueRegistry, placeholderHelper); polarisPropertySourceManager, springValueRegistry, placeholderHelper);
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class);
ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);

@ -21,14 +21,14 @@ package com.tencent.cloud.polaris.config.condition;
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.enums.RefreshType;
import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import org.junit.jupiter.api.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ConditionalOnConfigReflectEnabledTest { public class ConditionalOnConfigReflectEnabledTest {
@Test @Test
public void testReflectEnable() { public void testReflectEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner() ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
@ -59,12 +59,12 @@ public class ConditionalOnConfigReflectEnabledTest {
assertThat(context).hasSingleBean(PlaceholderHelper.class); assertThat(context).hasSingleBean(PlaceholderHelper.class);
assertThat(context).hasSingleBean(SpringValueRegistry.class); assertThat(context).hasSingleBean(SpringValueRegistry.class);
assertThat(context).hasSingleBean(SpringValueProcessor.class); assertThat(context).hasSingleBean(SpringValueProcessor.class);
assertThat(context).hasSingleBean(PolarisReflectPropertySourceAutoRefresher.class); assertThat(context).hasSingleBean(PolarisReflectConfigPropertyAutoRefresher.class);
}); });
} }
@Test @Test
public void testWithoutReflectEnable() { public void testWithoutReflectEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner() ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
@ -77,7 +77,7 @@ public class ConditionalOnConfigReflectEnabledTest {
assertThat(context).hasSingleBean(PolarisConfigProperties.class); assertThat(context).hasSingleBean(PolarisConfigProperties.class);
assertThat(context).hasSingleBean(PolarisPropertySourceManager.class); assertThat(context).hasSingleBean(PolarisPropertySourceManager.class);
assertThat(context).hasSingleBean(ContextRefresher.class); assertThat(context).hasSingleBean(ContextRefresher.class);
assertThat(context).hasSingleBean(PolarisRefreshContextPropertySourceAutoRefresher.class); assertThat(context).hasSingleBean(PolarisRefreshContextConfigPropertyAutoRefresher.class);
}); });
} }
} }

Loading…
Cancel
Save