parent
84a98de136
commit
28dc95b9a0
@ -1,94 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.polaris.config.adapter;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.core.PriorityOrdered;
|
|
||||||
import org.springframework.lang.NonNull;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mainly used to detect whether the annotation class {@link org.springframework.cloud.context.config.annotation.RefreshScope}
|
|
||||||
* exists, and whether the user has configured beans using this annotation in their business system.
|
|
||||||
* If the annotation {@code @RefreshScope} exists and is used, the auto-optimization will be triggered
|
|
||||||
* in listener {@link com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener}.
|
|
||||||
*
|
|
||||||
* <p>This bean will only be created and initialized when the config refresh type is {@code RefreshType.REFLECT}.
|
|
||||||
*
|
|
||||||
* @author jarvisxiong
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
public class PolarisConfigRefreshScopeAnnotationDetector implements BeanPostProcessor, InitializingBean, PriorityOrdered {
|
|
||||||
|
|
||||||
private final AtomicBoolean isRefreshScopeAnnotationUsed = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
private Class refreshScopeAnnotationClass;
|
|
||||||
|
|
||||||
private String annotatedRefreshScopeBeanName;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object postProcessBeforeInitialization(@NonNull Object bean, @NonNull String beanName)
|
|
||||||
throws BeansException {
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName)
|
|
||||||
throws BeansException {
|
|
||||||
if (isRefreshScopeAnnotationUsed() || refreshScopeAnnotationClass == null) {
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
Annotation[] refreshScopeAnnotations = bean.getClass().getAnnotationsByType(refreshScopeAnnotationClass);
|
|
||||||
if (refreshScopeAnnotations.length > 0) {
|
|
||||||
if (isRefreshScopeAnnotationUsed.compareAndSet(false, true)) {
|
|
||||||
annotatedRefreshScopeBeanName = beanName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
try {
|
|
||||||
refreshScopeAnnotationClass = Class.forName(
|
|
||||||
"org.springframework.cloud.context.config.annotation.RefreshScope",
|
|
||||||
false,
|
|
||||||
getClass().getClassLoader());
|
|
||||||
}
|
|
||||||
catch (ClassNotFoundException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOrder() {
|
|
||||||
return Ordered.LOWEST_PRECEDENCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isRefreshScopeAnnotationUsed() {
|
|
||||||
return isRefreshScopeAnnotationUsed.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAnnotatedRefreshScopeBeanName() {
|
|
||||||
return annotatedRefreshScopeBeanName;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,136 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.polaris.config.listener;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
|
|
||||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
|
||||||
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
||||||
import org.springframework.context.ApplicationListener;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.event.ContextRefreshedEvent;
|
|
||||||
import org.springframework.core.env.MapPropertySource;
|
|
||||||
import org.springframework.core.env.MutablePropertySources;
|
|
||||||
import org.springframework.lang.NonNull;
|
|
||||||
|
|
||||||
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* When {@link PolarisConfigRefreshScopeAnnotationDetector} detects that
|
|
||||||
* the annotation {@code @RefreshScope} exists and is used, but the config refresh type
|
|
||||||
* {@code spring.cloud.polaris.config.refresh-type} is still {@code RefreshType.REFLECT}, then the framework will
|
|
||||||
* automatically switch the config refresh type to {@code RefreshType.REFRESH_CONTEXT}.
|
|
||||||
*
|
|
||||||
* <p>The purpose of this optimization is to omit additional configuration, and facilitate for users to use the
|
|
||||||
* dynamic configuration refresh strategy of Spring Cloud Context.</p>
|
|
||||||
*
|
|
||||||
* @author jarvisxiong
|
|
||||||
*/
|
|
||||||
public class PolarisConfigRefreshOptimizationListener implements ApplicationListener<ContextRefreshedEvent> {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigRefreshOptimizationListener.class);
|
|
||||||
|
|
||||||
private static final String CONFIG_REFRESH_TYPE_PROPERTY = "configRefreshTypeProperty";
|
|
||||||
|
|
||||||
private static final String REFLECT_REBINDER_BEAN_NAME = "affectedConfigurationPropertiesRebinder";
|
|
||||||
|
|
||||||
private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher";
|
|
||||||
|
|
||||||
private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher";
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
|
|
||||||
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) event.getApplicationContext();
|
|
||||||
PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
|
||||||
boolean isRefreshScopeAnnotationUsed = detector.isRefreshScopeAnnotationUsed();
|
|
||||||
String annotatedRefreshScopeBeanName = detector.getAnnotatedRefreshScopeBeanName();
|
|
||||||
// using System.setProperty to set spring.cloud.polaris.config.refresh-type
|
|
||||||
String value = System.getProperty("spring.cloud.polaris.config.refresh-type");
|
|
||||||
boolean isSystemSetRefreshType = RefreshType.REFRESH_CONTEXT.toString().equalsIgnoreCase(value);
|
|
||||||
// a bean is using @RefreshScope, but the config refresh type is still [reflect], switch automatically
|
|
||||||
if (isRefreshScopeAnnotationUsed || isSystemSetRefreshType) {
|
|
||||||
if (isRefreshScopeAnnotationUsed) {
|
|
||||||
LOGGER.warn("Detected that the bean [{}] is using @RefreshScope annotation, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context].", annotatedRefreshScopeBeanName);
|
|
||||||
}
|
|
||||||
if (isSystemSetRefreshType) {
|
|
||||||
LOGGER.warn("Detected that using System.setProperty to set spring.cloud.polaris.config.refresh-type = refresh_context, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context].");
|
|
||||||
}
|
|
||||||
switchConfigRefreshTypeProperty(applicationContext);
|
|
||||||
modifyPolarisConfigPropertiesBean(applicationContext);
|
|
||||||
// remove related bean of type [reflect]
|
|
||||||
removeRelatedBeansOfReflect(applicationContext);
|
|
||||||
// register a new refresher bean of type [refresh_context]
|
|
||||||
registerRefresherBeanOfRefreshContext(applicationContext);
|
|
||||||
// add the new refresher to context as a listener
|
|
||||||
addRefresherBeanAsListener(applicationContext);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void switchConfigRefreshTypeProperty(ConfigurableApplicationContext applicationContext) {
|
|
||||||
MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
|
|
||||||
propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void modifyPolarisConfigPropertiesBean(ConfigurableApplicationContext applicationContext) {
|
|
||||||
PolarisConfigProperties polarisConfigProperties = applicationContext.getBean(PolarisConfigProperties.class);
|
|
||||||
polarisConfigProperties.setRefreshType(RefreshType.REFRESH_CONTEXT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeRelatedBeansOfReflect(ConfigurableApplicationContext applicationContext) {
|
|
||||||
try {
|
|
||||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
|
|
||||||
beanFactory.removeBeanDefinition(REFLECT_REFRESHER_BEAN_NAME);
|
|
||||||
beanFactory.removeBeanDefinition(REFLECT_REBINDER_BEAN_NAME);
|
|
||||||
}
|
|
||||||
catch (BeansException e) {
|
|
||||||
// If there is a removeBean exception in this code, do not affect the main process startup. Some user usage may cause the polarisReflectPropertySourceAutoRefresher to not load, and the removeBeanDefinition will report an error
|
|
||||||
LOGGER.debug("removeRelatedBeansOfReflect occur error:", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerRefresherBeanOfRefreshContext(ConfigurableApplicationContext applicationContext) {
|
|
||||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
|
|
||||||
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
|
|
||||||
beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class);
|
|
||||||
PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class);
|
|
||||||
ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class);
|
|
||||||
ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues();
|
|
||||||
constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties);
|
|
||||||
constructorArgumentValues.addIndexedArgumentValue(1, contextRefresher);
|
|
||||||
beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void addRefresherBeanAsListener(ConfigurableApplicationContext applicationContext) {
|
|
||||||
PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME);
|
|
||||||
applicationContext.addApplicationListener(refresher);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,101 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.polaris.config.adapter;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration;
|
|
||||||
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
|
|
||||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
|
||||||
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
|
|
||||||
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.as;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test for {@link PolarisConfigRefreshScopeAnnotationDetector}.
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
public class PolarisConfigRefreshScopeAnnotationDetectorTest {
|
|
||||||
|
|
||||||
private static Class refreshScopeAnnotationClass = null;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
refreshScopeAnnotationClass = Class.forName(
|
|
||||||
"org.springframework.cloud.context.config.annotation.RefreshScope",
|
|
||||||
false,
|
|
||||||
PolarisConfigRefreshScopeAnnotationDetectorTest.class.getClassLoader());
|
|
||||||
}
|
|
||||||
catch (ClassNotFoundException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUseRefreshScope() {
|
|
||||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
||||||
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class))
|
|
||||||
.withBean("testBeanWithRefreshScope", TestBeanWithRefreshScope.class)
|
|
||||||
.withPropertyValues("spring.application.name=" + "polarisConfigRefreshScopeAnnotationDetectorTest")
|
|
||||||
.withPropertyValues("server.port=" + 8080)
|
|
||||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
|
||||||
.withPropertyValues("spring.cloud.polaris.config.connect-remote-server=false");
|
|
||||||
contextRunner.run(context -> {
|
|
||||||
assertThat(context).hasSingleBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
|
||||||
PolarisConfigRefreshScopeAnnotationDetector detector = context.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
|
||||||
assertThat(detector.isRefreshScopeAnnotationUsed()).isTrue();
|
|
||||||
assertThat(detector.getAnnotatedRefreshScopeBeanName()).isEqualTo("scopedTarget.testBeanWithRefreshScope");
|
|
||||||
assertThat(detector).extracting("refreshScopeAnnotationClass", as(InstanceOfAssertFactories.type(Class.class)))
|
|
||||||
.isEqualTo(refreshScopeAnnotationClass);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNotUseRefreshScope() {
|
|
||||||
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
|
||||||
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
|
|
||||||
.withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class))
|
|
||||||
.withPropertyValues("spring.application.name=" + "polarisConfigRefreshScopeAnnotationDetectorTest")
|
|
||||||
.withPropertyValues("server.port=" + 8080)
|
|
||||||
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
|
|
||||||
.withPropertyValues("spring.cloud.polaris.config.connect-remote-server=false");
|
|
||||||
contextRunner.run(context -> {
|
|
||||||
assertThat(context).hasSingleBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
|
||||||
PolarisConfigRefreshScopeAnnotationDetector detector = context.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
|
|
||||||
assertThat(detector.isRefreshScopeAnnotationUsed()).isFalse();
|
|
||||||
assertThat(detector.getAnnotatedRefreshScopeBeanName()).isNull();
|
|
||||||
assertThat(detector).extracting("refreshScopeAnnotationClass", as(InstanceOfAssertFactories.type(Class.class)))
|
|
||||||
.isEqualTo(refreshScopeAnnotationClass);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@RefreshScope
|
|
||||||
protected static class TestBeanWithRefreshScope {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,153 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.polaris.config.listener;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher;
|
|
||||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
|
||||||
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ChangeType;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.context.support.AbstractApplicationContext;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
|
|
||||||
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test for {@link PolarisConfigRefreshOptimizationListener}.
|
|
||||||
*/
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerNotTriggeredTest.TestApplication.class,
|
|
||||||
properties = {
|
|
||||||
"server.port=48081",
|
|
||||||
"spring.cloud.polaris.address=grpc://127.0.0.1:10081",
|
|
||||||
"spring.cloud.polaris.config.connect-remote-server=false",
|
|
||||||
"spring.cloud.polaris.config.refresh-type=reflect",
|
|
||||||
"spring.config.location = classpath:application-test.yml"
|
|
||||||
})
|
|
||||||
public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest {
|
|
||||||
|
|
||||||
private static final String REFLECT_REFRESHER_BEAN_NAME = "polarisReflectPropertySourceAutoRefresher";
|
|
||||||
|
|
||||||
private static final String TEST_NAMESPACE = "testNamespace";
|
|
||||||
|
|
||||||
private static final String TEST_SERVICE_NAME = "testServiceName";
|
|
||||||
|
|
||||||
private static final String TEST_FILE_NAME = "application.properties";
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ConfigurableApplicationContext context;
|
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void beforeAll() {
|
|
||||||
PolarisPropertySourceManager.clearPropertySources();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNotSwitchConfigRefreshType() {
|
|
||||||
RefreshType actualRefreshType = context.getEnvironment()
|
|
||||||
.getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class);
|
|
||||||
assertThat(actualRefreshType).isEqualTo(RefreshType.REFLECT);
|
|
||||||
PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class);
|
|
||||||
assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFLECT);
|
|
||||||
assertThat(context.containsBean(REFLECT_REFRESHER_BEAN_NAME)).isTrue();
|
|
||||||
PolarisRefreshAffectedContextRefresher refresher = context
|
|
||||||
.getBean(REFLECT_REFRESHER_BEAN_NAME, PolarisRefreshAffectedContextRefresher.class);
|
|
||||||
assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testConfigFileChanged() {
|
|
||||||
Map<String, Object> content = new HashMap<>();
|
|
||||||
content.put("k1", "v1");
|
|
||||||
content.put("k2", "v2");
|
|
||||||
content.put("k3", "v3");
|
|
||||||
MockedConfigKVFile file = new MockedConfigKVFile(content);
|
|
||||||
|
|
||||||
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
|
|
||||||
file, content);
|
|
||||||
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
||||||
|
|
||||||
PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class);
|
|
||||||
PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher);
|
|
||||||
|
|
||||||
refresher.setRegistered(false);
|
|
||||||
spyRefresher.onApplicationEvent(null);
|
|
||||||
|
|
||||||
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
|
|
||||||
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);
|
|
||||||
ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED);
|
|
||||||
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
|
||||||
changeInfos.put("k1", changeInfo);
|
|
||||||
changeInfos.put("k2", changeInfo3);
|
|
||||||
changeInfos.put("k4", changeInfo2);
|
|
||||||
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
|
||||||
file.fireChangeListener(event);
|
|
||||||
|
|
||||||
ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class);
|
|
||||||
when(mockContextRefresher.refresh()).thenReturn(event.changedKeys());
|
|
||||||
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k1");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k2");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k4");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshConfigurationProperties(event.changedKeys());
|
|
||||||
}
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
protected static class TestApplication {
|
|
||||||
|
|
||||||
@Primary
|
|
||||||
@Bean
|
|
||||||
public ContextRefresher contextRefresher() {
|
|
||||||
return mock(ContextRefresher.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Component
|
|
||||||
protected static class TestBeanWithoutRefreshScope {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,155 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* 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.tencent.cloud.polaris.config.listener;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
|
|
||||||
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
|
|
||||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
|
||||||
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ChangeType;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
|
|
||||||
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
||||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
import org.springframework.context.support.AbstractApplicationContext;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|
||||||
|
|
||||||
import static com.tencent.cloud.polaris.config.condition.ReflectRefreshTypeCondition.POLARIS_CONFIG_REFRESH_TYPE;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test for {@link PolarisConfigRefreshOptimizationListener}.
|
|
||||||
*/
|
|
||||||
@ExtendWith(SpringExtension.class)
|
|
||||||
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = PolarisConfigRefreshOptimizationListenerTriggeredTest.TestApplication.class,
|
|
||||||
properties = {
|
|
||||||
"server.port=48081",
|
|
||||||
"spring.cloud.polaris.address=grpc://127.0.0.1:10081",
|
|
||||||
"spring.cloud.polaris.config.connect-remote-server=false",
|
|
||||||
"spring.cloud.polaris.config.refresh-type=reflect",
|
|
||||||
"spring.config.location = classpath:application-test.yml"
|
|
||||||
})
|
|
||||||
public class PolarisConfigRefreshOptimizationListenerTriggeredTest {
|
|
||||||
|
|
||||||
private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher";
|
|
||||||
|
|
||||||
private static final String TEST_NAMESPACE = "testNamespace";
|
|
||||||
|
|
||||||
private static final String TEST_SERVICE_NAME = "testServiceName";
|
|
||||||
|
|
||||||
private static final String TEST_FILE_NAME = "application.properties";
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ConfigurableApplicationContext context;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
PolarisPropertySourceManager.clearPropertySources();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSwitchConfigRefreshType() {
|
|
||||||
RefreshType actualRefreshType = context.getEnvironment()
|
|
||||||
.getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class);
|
|
||||||
assertThat(actualRefreshType).isEqualTo(RefreshType.REFRESH_CONTEXT);
|
|
||||||
PolarisConfigProperties polarisConfigProperties = context.getBean(PolarisConfigProperties.class);
|
|
||||||
assertThat(polarisConfigProperties.getRefreshType()).isEqualTo(RefreshType.REFRESH_CONTEXT);
|
|
||||||
assertThat(context.containsBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME)).isTrue();
|
|
||||||
PolarisRefreshEntireContextRefresher refresher = context
|
|
||||||
.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, PolarisRefreshEntireContextRefresher.class);
|
|
||||||
assertThat(((AbstractApplicationContext) context).getApplicationListeners().contains(refresher)).isTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testConfigFileChanged() {
|
|
||||||
Map<String, Object> content = new HashMap<>();
|
|
||||||
content.put("k1", "v1");
|
|
||||||
content.put("k2", "v2");
|
|
||||||
content.put("k3", "v3");
|
|
||||||
MockedConfigKVFile file = new MockedConfigKVFile(content);
|
|
||||||
|
|
||||||
PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME,
|
|
||||||
file, content);
|
|
||||||
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
||||||
|
|
||||||
PolarisRefreshEntireContextRefresher refresher = context.getBean(PolarisRefreshEntireContextRefresher.class);
|
|
||||||
PolarisRefreshEntireContextRefresher spyRefresher = Mockito.spy(refresher);
|
|
||||||
|
|
||||||
refresher.setRegistered(false);
|
|
||||||
spyRefresher.onApplicationEvent(null);
|
|
||||||
|
|
||||||
ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED);
|
|
||||||
ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED);
|
|
||||||
ConfigPropertyChangeInfo changeInfo3 = new ConfigPropertyChangeInfo("k2", "v2", null, ChangeType.DELETED);
|
|
||||||
Map<String, ConfigPropertyChangeInfo> changeInfos = new HashMap<>();
|
|
||||||
changeInfos.put("k1", changeInfo);
|
|
||||||
changeInfos.put("k2", changeInfo3);
|
|
||||||
changeInfos.put("k4", changeInfo2);
|
|
||||||
ConfigKVFileChangeEvent event = new ConfigKVFileChangeEvent(changeInfos);
|
|
||||||
file.fireChangeListener(event);
|
|
||||||
|
|
||||||
ContextRefresher mockContextRefresher = context.getBean(ContextRefresher.class);
|
|
||||||
when(mockContextRefresher.refresh()).thenReturn(event.changedKeys());
|
|
||||||
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k1");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k2");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshSpringValue("k4");
|
|
||||||
Mockito.verify(spyRefresher, Mockito.times(1))
|
|
||||||
.refreshConfigurationProperties(event.changedKeys());
|
|
||||||
}
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
protected static class TestApplication {
|
|
||||||
|
|
||||||
@Primary
|
|
||||||
@Bean
|
|
||||||
public ContextRefresher contextRefresher() {
|
|
||||||
return mock(ContextRefresher.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Component
|
|
||||||
@RefreshScope
|
|
||||||
protected static class TestBeanWithRefreshScope {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,335 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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.tencent.cloud.polaris.config.spring.annotation;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration;
|
||||||
|
import com.tencent.cloud.polaris.config.enums.RefreshType;
|
||||||
|
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link SpringValueProcessor}.
|
||||||
|
*
|
||||||
|
* @author Shedfree Wu
|
||||||
|
*/
|
||||||
|
public class RefreshScopeSpringProcessorTest {
|
||||||
|
|
||||||
|
private static ServerSocket serverSocket;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void beforeAll() {
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
serverSocket = new ServerSocket(8093);
|
||||||
|
serverSocket.accept();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void afterAll() throws IOException {
|
||||||
|
if (Objects.nonNull(serverSocket)) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void springValueFiledProcessorTest() {
|
||||||
|
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(ValueTest.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestConfig2.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestConfig3.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestConfig4.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestConfig5.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestBeanProperties1.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(TestBeanProperties2.class))
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class))
|
||||||
|
.withAllowBeanDefinitionOverriding(true)
|
||||||
|
.withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest")
|
||||||
|
.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")
|
||||||
|
.withPropertyValues("timeout=10000");
|
||||||
|
contextRunner.run(context -> {
|
||||||
|
SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class);
|
||||||
|
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("key.not.exist")).isFalse();
|
||||||
|
// @RefreshScope on @Component bean, @Value on field
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("timeout")).isTrue();
|
||||||
|
// not exact match
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("timeout.test")).isFalse();
|
||||||
|
// @RefreshScope on @Component bean, @Value on method
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("name")).isTrue();
|
||||||
|
// @RefreshScope and @Bean on method, @Value on field
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.bean.name")).isTrue();
|
||||||
|
// @RefreshScope and @Bean on method, @Value on method
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.bean.timeout")).isTrue();
|
||||||
|
// @RefreshScope and @Bean on method, @Value on parameter
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.param.name")).isTrue();
|
||||||
|
// @RefreshScope and @Bean on method, @ConfigurationProperties bean on method parameter
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties1.name")).isTrue();
|
||||||
|
// @RefreshScope and @Bean on method, @ConfigurationProperties bean in class
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.name")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.inner.name")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.set")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.list")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.list[0]")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.array")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.array[0]")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.map")).isTrue();
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.map.key")).isTrue();
|
||||||
|
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.properties2.notExist")).isFalse();
|
||||||
|
// @RefreshScope and @Bean on method, @Value bean in class
|
||||||
|
assertThat(springValueRegistry.isRefreshScopeKey("test.bean5.name")).isTrue();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
static class PolarisConfigAutoConfiguration {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
|
public BeanFactory getBeanFactory() {
|
||||||
|
return beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RefreshScope
|
||||||
|
private static class ValueTest {
|
||||||
|
|
||||||
|
ValueTest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String name;
|
||||||
|
@Value("${timeout:1000}")
|
||||||
|
private int timeout;
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Value("${name:1000}")
|
||||||
|
public void setName(String name) {
|
||||||
|
ValueTest.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfig2 {
|
||||||
|
@Bean
|
||||||
|
@RefreshScope
|
||||||
|
public TestBean testBean2() {
|
||||||
|
return new TestBean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfig3 {
|
||||||
|
@Bean
|
||||||
|
@RefreshScope
|
||||||
|
public TestBean testBean3(@Value("${test.param.name:}") String name) {
|
||||||
|
return new TestBean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfig4 {
|
||||||
|
@Bean
|
||||||
|
@RefreshScope
|
||||||
|
public TestBean testBean4(TestBeanProperties1 testBeanProperties1) {
|
||||||
|
return new TestBean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfig5 {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestBeanProperties2 testBeanProperties2;
|
||||||
|
|
||||||
|
@Value("${test.bean5.name:}")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@RefreshScope
|
||||||
|
public TestBean testBean5() {
|
||||||
|
TestBean testBean = new TestBean();
|
||||||
|
testBean.setName(testBeanProperties2.getName());
|
||||||
|
return testBean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestBean {
|
||||||
|
|
||||||
|
@Value("${test.bean.name:}")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private int timeout;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Value("${test.bean.timeout:0}")
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "test.properties1")
|
||||||
|
static class TestBeanProperties1 {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties("test.properties2")
|
||||||
|
static class TestBeanProperties2 {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private HashSet<String> set;
|
||||||
|
|
||||||
|
private ArrayList<String> list;
|
||||||
|
|
||||||
|
private String[] array;
|
||||||
|
|
||||||
|
private HashMap<String, String> map;
|
||||||
|
|
||||||
|
private InnerProperties inner;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashSet<String> getSet() {
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSet(HashSet<String> set) {
|
||||||
|
this.set = set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<String> getList() {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setList(ArrayList<String> list) {
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getArray() {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArray(String[] array) {
|
||||||
|
this.array = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap<String, String> getMap() {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMap(HashMap<String, String> map) {
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InnerProperties getInner() {
|
||||||
|
return inner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInner(InnerProperties inner) {
|
||||||
|
this.inner = inner;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class InnerProperties {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue