Using ConfigurationPropertiesRebinder when behavior is default

pull/2437/head
Freeman Lau 3 years ago
parent 9c4dc69cce
commit 9882d5727c

@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package com.alibaba.cloud.nacos;
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory;
import com.alibaba.cloud.nacos.refresh.SmartConfigurationPropertiesRebinder;
import com.alibaba.cloud.nacos.refresh.condition.ConditionalOnNonDefaultBehavior;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -73,8 +74,11 @@ public class NacosConfigAutoConfiguration {
@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);
}

@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package com.alibaba.cloud.nacos;
import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator;
import com.alibaba.cloud.nacos.refresh.SmartConfigurationPropertiesRebinder;
import com.alibaba.cloud.nacos.refresh.condition.ConditionalOnNonDefaultBehavior;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -54,14 +55,16 @@ public class NacosConfigBootstrapConfiguration {
return new NacosPropertySourceLocator(nacosConfigManager);
}
/**
* Compatible with bootstrap way to start.
*/
@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);
}

@ -33,13 +33,15 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import static com.alibaba.cloud.nacos.refresh.RefreshBehavior.ALL_BEANS;
/**
* Extend {@link ConfigurationPropertiesRebinder}.
* <p>
* Spring team doesn't seem to support single {@link ConfigurationPropertiesBean} refresh.
* <p>
* SmartConfigurationPropertiesRebinder can refresh specific
* {@link ConfigurationPropertiesBean} base on change keys.
* {@link ConfigurationPropertiesBean} base on the change keys.
* <p>
* <strong> NOTE: We still use Spring's default behavior (full refresh) as default
* behavior, This feature can be considered an advanced feature, it may not be as stable
@ -81,7 +83,7 @@ public class SmartConfigurationPropertiesRebinder
this.applicationContext = applicationContext;
this.refreshBehavior = this.applicationContext.getEnvironment().getProperty(
"spring.cloud.nacos.config.refresh-behavior", RefreshBehavior.class,
RefreshBehavior.ALL_BEANS);
ALL_BEANS);
}
@Override

@ -0,0 +1,41 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.nacos.refresh.condition;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.alibaba.cloud.nacos.refresh.RefreshBehavior;
import org.springframework.context.annotation.Conditional;
/**
* Condition on {@link RefreshBehavior} is <strong>NOT</strong> default.
*
* @author freeman
* @since 2021.0.1.1
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(NonDefaultBehaviorCondition.class)
public @interface ConditionalOnNonDefaultBehavior {
}

@ -0,0 +1,48 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.nacos.refresh.condition;
import com.alibaba.cloud.nacos.refresh.RefreshBehavior;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import static com.alibaba.cloud.nacos.refresh.RefreshBehavior.ALL_BEANS;
/**
* @author freeman
* @since 2021.0.1.1
*/
public class NonDefaultBehaviorCondition extends SpringBootCondition {
private static final RefreshBehavior DEFAULT_REFRESH_BEHAVIOR = ALL_BEANS;
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
RefreshBehavior behavior = context.getEnvironment().getProperty(
"spring.cloud.nacos.config.refresh-behavior", RefreshBehavior.class,
DEFAULT_REFRESH_BEHAVIOR);
if (DEFAULT_REFRESH_BEHAVIOR == behavior) {
return ConditionOutcome.noMatch("no matched");
}
return ConditionOutcome.match("matched");
}
}

@ -20,33 +20,37 @@ import com.alibaba.cloud.nacos.refresh.RefreshBehavior;
import com.alibaba.cloud.nacos.refresh.SmartConfigurationPropertiesRebinder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import static com.alibaba.cloud.nacos.SmartConfigurationPropertiesRebinderIntegrationTest.TestConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
/**
* {@link SmartConfigurationPropertiesRebinder} tester.
*
* @author freeman
*/
@SpringBootTest(classes = TestConfig.class, webEnvironment = NONE, properties = {
"spring.cloud.nacos.config.refresh-behavior=specific_bean",
"spring.cloud.nacos.server-addr=123.123.123.123:8848",
"spring.cloud.nacos.config.import-check.enabled=false"
})
public class SmartConfigurationPropertiesRebinderIntegrationTest {
@Autowired
private ConfigurationPropertiesRebinder rebinder;
ConfigurableApplicationContext context;
@Test
public void testUsingSmartConfigurationPropertiesRebinder() {
public void testUsingSmartConfigurationPropertiesRebinder_whenBehaviorIsNotDefault() {
context = new SpringApplicationBuilder(RebinderConfiguration.class)
.web(WebApplicationType.NONE)
.properties("spring.cloud.nacos.config.refresh-behavior=specific_bean")
.properties("spring.cloud.nacos.server-addr=123.123.123.123:8848")
.properties("spring.cloud.nacos.config.import-check.enabled=false").run();
ConfigurationPropertiesRebinder rebinder = context
.getBean(ConfigurationPropertiesRebinder.class);
assertThat(rebinder.getClass())
.isEqualTo(SmartConfigurationPropertiesRebinder.class);
@ -55,10 +59,23 @@ public class SmartConfigurationPropertiesRebinderIntegrationTest {
assertThat(refreshBehavior).isEqualTo(RefreshBehavior.SPECIFIC_BEAN);
}
@Test
public void testUsingConfigurationPropertiesRebinder_whenBehaviorIsDefault() {
context = new SpringApplicationBuilder(RebinderConfiguration.class)
.web(WebApplicationType.NONE)
.properties("spring.cloud.nacos.server-addr=123.123.123.123:8848")
.properties("spring.cloud.nacos.config.import-check.enabled=false").run();
ConfigurationPropertiesRebinder rebinder = context
.getBean(ConfigurationPropertiesRebinder.class);
assertThat(rebinder.getClass()).isEqualTo(ConfigurationPropertiesRebinder.class);
}
@Configuration
@ImportAutoConfiguration({ NacosConfigAutoConfiguration.class })
@EnableAutoConfiguration
public static class TestConfig {
public static class RebinderConfiguration {
}

Loading…
Cancel
Save