feature:add @ConditionalOnConfigReflectEnabled annotation

pull/496/head
wulingxiao 3 years ago
parent c4605d835e
commit f4b8c6e0ad

@ -18,6 +18,8 @@
package com.tencent.cloud.polaris.config.condition;
import java.util.Objects;
import com.tencent.cloud.polaris.config.enums.RefreshType;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
@ -38,18 +40,16 @@ public class ConfigReflectCondition extends SpringBootCondition {
public static final String POLARIS_CONFIG_REFRESH_TYPE = "spring.cloud.polaris.config.refresh-type";
/**
* Refresh type default value.
* Reflect refresh type value.
*/
private static final RefreshType DEFAULT_REFRESH_TYPE = RefreshType.REFRESH_CONTEXT;
private static final RefreshType REFLECT_TYPE = RefreshType.REFLECT;
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
RefreshType refreshType = context.getEnvironment().getProperty(
POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class,
DEFAULT_REFRESH_TYPE);
if (DEFAULT_REFRESH_TYPE == refreshType) {
return ConditionOutcome.noMatch("no matched");
RefreshType refreshType = context.getEnvironment().getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class);
if (!Objects.isNull(refreshType) && REFLECT_TYPE == refreshType) {
return ConditionOutcome.match("matched");
}
return ConditionOutcome.match("matched");
return ConditionOutcome.noMatch("no matched");
}
}

@ -24,6 +24,7 @@ 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.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;
@ -43,19 +44,18 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class ConditionalOnConfigReflectEnabledTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
.withPropertyValues("server.port=" + 8080)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
@Test
public void testReflectEnable() {
contextRunner.withPropertyValues("spring.cloud.polaris.config.refresh-type=reflect");
contextRunner.withPropertyValues("spring.cloud.polaris.config.enabled=true");
this.contextRunner.run(context -> {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
.withPropertyValues("server.port=" + 8080)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.config.refresh-type=" + RefreshType.REFLECT)
.withPropertyValues("spring.cloud.polaris.config.enabled=true");
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PlaceholderHelper.class);
assertThat(context).hasSingleBean(SpringValueRegistry.class);
assertThat(context).hasSingleBean(SpringValueProcessor.class);
@ -65,8 +65,15 @@ public class ConditionalOnConfigReflectEnabledTest {
@Test
public void testWithoutReflectEnable() {
contextRunner.withPropertyValues("spring.cloud.polaris.config.enabled=true");
this.contextRunner.run(context -> {
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
.withPropertyValues("server.port=" + 8080)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.config.enabled=true");
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisConfigProperties.class);
assertThat(context).hasSingleBean(PolarisPropertySourceManager.class);
assertThat(context).hasSingleBean(ContextRefresher.class);

Loading…
Cancel
Save