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;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
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.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
@ -62,33 +63,6 @@ public class PolarisConfigAutoConfiguration {
return new PolarisConfigChangeEventListener();
}
@Bean
@ConditionalOnConfigReflectEnabled
public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry();
}
@Bean
@ConditionalOnConfigReflectEnabled
public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper();
}
@Bean
@ConditionalOnConfigReflectEnabled
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper, SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
}
@Bean
@ConditionalOnConfigReflectEnabled
public PolarisPropertySourceRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager,
SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
return new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper);
}
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
@ConditionalOnNonDefaultBehavior
@ -101,8 +75,36 @@ public class PolarisConfigAutoConfiguration {
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisPropertySourceRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) {
return new PolarisRefreshContextPropertySourceAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher);
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) {
return new PolarisRefreshContextConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher);
}
@ConditionalOnConfigReflectEnabled
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(PolarisConfigAutoConfiguration.class)
public static class PolarisReflectRefresherAutoConfiguration {
@Bean
public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry();
}
@Bean
public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper();
}
@Bean
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper,
SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
}
@Bean
public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
return new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper);
}
}
}

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

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

@ -38,15 +38,15 @@ import org.springframework.context.ApplicationContextAware;
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.
*
* @author lingxiao.wlx
*/
public class PolarisReflectPropertySourceAutoRefresher extends PolarisPropertySourceAutoRefresher
public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigPropertyAutoRefresher
implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectPropertySourceAutoRefresher.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectConfigPropertyAutoRefresher.class);
private final SpringValueRegistry springValueRegistry;
@ -58,9 +58,9 @@ public class PolarisReflectPropertySourceAutoRefresher extends PolarisPropertySo
private TypeConverter typeConverter;
public PolarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
public PolarisReflectConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
super(polarisConfigProperties, polarisPropertySourceManager);
this.springValueRegistry = springValueRegistry;
this.placeholderHelper = placeholderHelper;

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

@ -47,7 +47,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* test for {@link PolarisReflectPropertySourceAutoRefresher}.
* test for {@link PolarisReflectConfigPropertyAutoRefresher}.
*
* @author lepdou 2022-06-11
*/
@ -70,7 +70,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Test
public void testConfigFileChanged() throws Exception {
PolarisReflectPropertySourceAutoRefresher refresher = new PolarisReflectPropertySourceAutoRefresher(polarisConfigProperties,
PolarisReflectConfigPropertyAutoRefresher refresher = new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties,
polarisPropertySourceManager, springValueRegistry, placeholderHelper);
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.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.PolarisConfigBootstrapAutoConfiguration;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectPropertySourceAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextPropertySourceAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.RefreshType;
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.SpringValueRegistry;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ConditionalOnConfigReflectEnabledTest {
@Test
public void testReflectEnable() {
public void testReflectEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
@ -59,12 +59,12 @@ public class ConditionalOnConfigReflectEnabledTest {
assertThat(context).hasSingleBean(PlaceholderHelper.class);
assertThat(context).hasSingleBean(SpringValueRegistry.class);
assertThat(context).hasSingleBean(SpringValueProcessor.class);
assertThat(context).hasSingleBean(PolarisReflectPropertySourceAutoRefresher.class);
assertThat(context).hasSingleBean(PolarisReflectConfigPropertyAutoRefresher.class);
});
}
@Test
public void testWithoutReflectEnable() {
public void testWithoutReflectEnabled() {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
@ -77,7 +77,7 @@ public class ConditionalOnConfigReflectEnabledTest {
assertThat(context).hasSingleBean(PolarisConfigProperties.class);
assertThat(context).hasSingleBean(PolarisPropertySourceManager.class);
assertThat(context).hasSingleBean(ContextRefresher.class);
assertThat(context).hasSingleBean(PolarisRefreshContextPropertySourceAutoRefresher.class);
assertThat(context).hasSingleBean(PolarisRefreshContextConfigPropertyAutoRefresher.class);
});
}
}

Loading…
Cancel
Save