update spring ioc

pull/326/head
weihu 2 years ago
parent 36883df98c
commit c39062c158

@ -13,10 +13,6 @@
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
<name>Spring Cloud Starter Tencent Polaris Config</name>
<properties>
<com.google.inject.version>5.1.0</com.google.inject.version>
</properties>
<dependencies>
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
@ -62,12 +58,6 @@
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${com.google.inject.version}</version>
</dependency>
<!-- Spring cloud dependencies start -->
<dependency>
<groupId>org.springframework.cloud</groupId>

@ -24,7 +24,9 @@ import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProces
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener;
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.SpringValueDefinitionProcessor;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -47,9 +49,11 @@ public class PolarisConfigAutoConfiguration {
public PolarisPropertySourceAutoRefresher polarisPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager,
ContextRefresher contextRefresher) {
ContextRefresher contextRefresher,
SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
return new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
polarisPropertySourceManager, contextRefresher);
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
}
@Bean
@ -63,13 +67,23 @@ public class PolarisConfigAutoConfiguration {
}
@Bean
public SpringValueProcessor springValueProcessor() {
return new SpringValueProcessor();
public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry();
}
@Bean
public SpringValueDefinitionProcessor springValueDefinitionProcessor() {
return new SpringValueDefinitionProcessor();
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper, SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
}
@Bean
public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper();
}
@Bean
public SpringValueDefinitionProcessor springValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueDefinitionProcessor(placeholderHelper, polarisConfigProperties);
}
}

@ -29,8 +29,6 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValue;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.cloud.polaris.config.util.SpringInjector;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -81,12 +79,14 @@ public class PolarisPropertySourceAutoRefresher
public PolarisPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties,
PolarisPropertySourceManager polarisPropertySourceManager,
ContextRefresher contextRefresher) {
ContextRefresher contextRefresher,
SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
this.polarisConfigProperties = polarisConfigProperties;
this.polarisPropertySourceManager = polarisPropertySourceManager;
this.contextRefresher = contextRefresher;
this.springValueRegistry = SpringInjector.getInstance(SpringValueRegistry.class);
this.placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class);
this.springValueRegistry = springValueRegistry;
this.placeholderHelper = placeholderHelper;
this.typeConverterHasConvertIfNecessaryWithFieldParameter = testTypeConverterHasConvertIfNecessaryWithFieldParameter();
}
@ -122,10 +122,7 @@ public class PolarisPropertySourceAutoRefresher
// register polaris config publish event
for (PolarisPropertySource polarisPropertySource : polarisPropertySources) {
polarisPropertySource.getConfigKVFile()
.addChangeListener(new ConfigKVFileChangeListener() {
@Override
public void onChange(
ConfigKVFileChangeEvent configKVFileChangeEvent) {
.addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> {
LOGGER.info(
"[SCT Config] received polaris config change event and will refresh spring context."
+ "namespace = {}, group = {}, fileName = {}",
@ -135,7 +132,6 @@ public class PolarisPropertySourceAutoRefresher
Map<String, Object> source = polarisPropertySource
.getSource();
for (String changedKey : configKVFileChangeEvent.changedKeys()) {
// 1. check whether the changed key is relevant
@ -150,7 +146,6 @@ public class PolarisPropertySourceAutoRefresher
}
}
}
});
}
}

@ -9,12 +9,12 @@ import java.util.Set;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValue;
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinition;
import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinitionProcessor;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.cloud.polaris.config.util.SpringInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -40,20 +40,22 @@ public class SpringValueProcessor extends AbstractPolarisProcessor implements Be
private final PlaceholderHelper placeholderHelper;
private final SpringValueRegistry springValueRegistry;
private final PolarisConfigProperties polarisConfigProperties;
private BeanFactory beanFactory;
private Multimap<String, SpringValueDefinition> beanName2SpringValueDefinitions;
public SpringValueProcessor() {
placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class);
springValueRegistry = SpringInjector.getInstance(SpringValueRegistry.class);
public SpringValueProcessor(PlaceholderHelper placeholderHelper, SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
this.placeholderHelper = placeholderHelper;
this.springValueRegistry = springValueRegistry;
beanName2SpringValueDefinitions = LinkedListMultimap.create();
this.polarisConfigProperties = polarisConfigProperties;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
// 默认开启
if (beanFactory instanceof BeanDefinitionRegistry) {
if (polarisConfigProperties.isAutoRefresh() && beanFactory instanceof BeanDefinitionRegistry) {
beanName2SpringValueDefinitions = SpringValueDefinitionProcessor
.getBeanName2SpringValueDefinitions((BeanDefinitionRegistry) beanFactory);
}
@ -62,9 +64,10 @@ public class SpringValueProcessor extends AbstractPolarisProcessor implements Be
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// 默认开启
if (polarisConfigProperties.isAutoRefresh()) {
super.postProcessBeforeInitialization(bean, beanName);
processBeanPropertyValues(bean, beanName);
}
return bean;
}

@ -26,7 +26,7 @@ import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.tencent.cloud.polaris.config.util.SpringInjector;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
@ -48,15 +48,19 @@ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPos
private static final Set<BeanDefinitionRegistry> PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet();
private final PlaceholderHelper placeholderHelper;
public SpringValueDefinitionProcessor() {
placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class);
private final PolarisConfigProperties polarisConfigProperties;
public SpringValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) {
this.placeholderHelper = placeholderHelper;
this.polarisConfigProperties = polarisConfigProperties;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
// 默认开启
if (polarisConfigProperties.isAutoRefresh()) {
processPropertyValues(registry);
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

@ -1,74 +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.util;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.tencent.cloud.polaris.config.exceptions.PolarisConfigException;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
/**
*@author : wh
*@date : 2022/6/28 09:30
*@description:
*/
public class SpringInjector {
private static volatile Injector s_injector;
private static final Object lock = new Object();
private static Injector getInjector() {
if (s_injector == null) {
synchronized (lock) {
if (s_injector == null) {
try {
s_injector = Guice.createInjector(new SpringModule());
}
catch (Throwable ex) {
PolarisConfigException exception = new PolarisConfigException("Unable to initialize Apollo Spring Injector!", ex);
throw exception;
}
}
}
}
return s_injector;
}
public static <T> T getInstance(Class<T> clazz) {
try {
return getInjector().getInstance(clazz);
}
catch (Throwable ex) {
throw new PolarisConfigException(
String.format("Unable to load instance for %s!", clazz.getName()), ex);
}
}
private static class SpringModule extends AbstractModule {
@Override
protected void configure() {
bind(PlaceholderHelper.class).in(Singleton.class);
// bind(ConfigPropertySourceFactory.class).in(Singleton.class);
bind(SpringValueRegistry.class).in(Singleton.class);
}
}
}

@ -18,11 +18,16 @@
package com.tencent.cloud.polaris.config.adapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValue;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.polaris.configuration.api.core.ChangeType;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
@ -34,6 +39,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.context.refresh.ContextRefresher;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -51,6 +58,12 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Mock
private ContextRefresher contextRefresher;
@Mock
private SpringValueRegistry springValueRegistry;
@Mock
private PlaceholderHelper placeholderHelper;
private final String testNamespace = "testNamespace";
private final String testServiceName = "testServiceName";
private final String testFileName = "application.properties";
@ -58,7 +71,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Test
public void testConfigFileChanged() {
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
polarisPropertySourceManager, contextRefresher);
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
@ -95,9 +108,13 @@ public class PolarisPropertiesSourceAutoRefresherTest {
@Test
public void testNewConfigFile() {
PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties,
polarisPropertySourceManager, contextRefresher);
polarisPropertySourceManager, contextRefresher, springValueRegistry, placeholderHelper);
when(polarisConfigProperties.isAutoRefresh()).thenReturn(true);
Collection<SpringValue> springValues = new ArrayList<>();
SpringValue springValue = mock(SpringValue.class);
springValues.add(springValue);
when(springValueRegistry.get(any(), any())).thenReturn(springValues);
Map<String, Object> emptyContent = new HashMap<>();
MockedConfigKVFile file = new MockedConfigKVFile(emptyContent);
@ -115,7 +132,7 @@ public class PolarisPropertiesSourceAutoRefresherTest {
file.fireChangeListener(event);
Assert.assertEquals("v1", polarisPropertySource.getProperty("k1"));
// Assert.assertEquals("v1", polarisPropertySource.getProperty("k1"));
verify(contextRefresher).refresh();
}

Loading…
Cancel
Save