diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fc40f18b..dfe2655b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,3 +24,4 @@ - [fix:fix router label feign interceptor order.](https://github.com/Tencent/spring-cloud-tencent/pull/1069) - [feat:added polaris weighted round robin load balancer.](https://github.com/Tencent/spring-cloud-tencent/pull/1062) - [fix:add deregister judgment.](https://github.com/Tencent/spring-cloud-tencent/pull/1074) +- [refactor:optimize discovery properties initialization.](https://github.com/Tencent/spring-cloud-tencent/pull/1077) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java index 8d4a751b6..43ca4b99a 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java @@ -29,7 +29,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; /** * Common configuration of discovery. @@ -38,9 +37,26 @@ import org.springframework.context.annotation.Import; */ @Configuration(proxyBeanMethods = false) @ConditionalOnPolarisEnabled -@Import({PolarisDiscoveryProperties.class, ConsulContextProperties.class, NacosContextProperties.class}) public class DiscoveryPropertiesAutoConfiguration { + @Bean + @ConditionalOnMissingBean + public PolarisDiscoveryProperties polarisDiscoveryProperties() { + return new PolarisDiscoveryProperties(); + } + + @Bean + @ConditionalOnMissingBean + public ConsulContextProperties consulContextProperties() { + return new ConsulContextProperties(); + } + + @Bean + @ConditionalOnMissingBean + public NacosContextProperties nacosContextProperties() { + return new NacosContextProperties(); + } + @Bean @ConditionalOnMissingBean public PolarisDiscoveryHandler polarisDiscoveryHandler(PolarisDiscoveryProperties polarisDiscoveryProperties, diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java index d77e36c6f..37a01df72 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java @@ -24,8 +24,6 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import static org.assertj.core.api.Assertions.assertThat; @@ -50,36 +48,4 @@ public class DiscoveryPropertiesAutoConfigurationTest { assertThat(context).hasSingleBean(DiscoveryConfigModifier.class); }); } - - @Test - public void testInit() { - ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner().withConfiguration( - AutoConfigurations.of( - PolarisContextAutoConfiguration.class, - TestConfiguration.class, - DiscoveryPropertiesAutoConfiguration.class)) - .withPropertyValues("spring.cloud.polaris.discovery.register=false") - .withPropertyValues("spring.cloud.consul.discovery.register=false") - .withPropertyValues("spring.cloud.consul.discovery.enabled=false"); - applicationContextRunner.run(context -> { - assertThat(context).hasSingleBean(DiscoveryPropertiesAutoConfiguration.class); - }); - } - - @Configuration - static class TestConfiguration { - @Bean - public PolarisDiscoveryProperties polarisDiscoveryProperties() { - PolarisDiscoveryProperties polarisDiscoveryProperties = new PolarisDiscoveryProperties(); - polarisDiscoveryProperties.setEnabled(false); - return polarisDiscoveryProperties; - } - - @Bean - public ConsulContextProperties consulContextProperties() { - ConsulContextProperties consulContextProperties = new ConsulContextProperties(); - consulContextProperties.setEnabled(true); - return consulContextProperties; - } - } }