Skip to content
Snippets Groups Projects
Unverified Commit fb3a6446 authored by xingfudeshi's avatar xingfudeshi Committed by GitHub
Browse files

optimize: the property bean may not be initialized while reading config value (#3316)

parent a2e6e97a
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 1999-2019 Seata.io Group.
*
* 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
*
* http://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 io.seata.spring.boot.autoconfigure;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import static io.seata.spring.boot.autoconfigure.StarterConstants.PROPERTY_BEAN_MAP;
/**
* @author xingfudeshi@gmail.com
*/
public class PropertyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean.getClass().isAnnotationPresent(ConfigurationProperties.class)) {
ConfigurationProperties configurationProperties = bean.getClass().getAnnotation(ConfigurationProperties.class);
String prefix = configurationProperties.prefix();
PROPERTY_BEAN_MAP.computeIfPresent(prefix, (k, v) -> {
v.complete(bean);
return v;
});
}
return bean;
}
}
......@@ -19,33 +19,6 @@ import io.seata.spring.annotation.GlobalTransactionScanner;
import io.seata.spring.annotation.datasource.SeataAutoDataSourceProxyCreator;
import io.seata.spring.annotation.datasource.SeataDataSourceBeanPostProcessor;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.properties.client.LockProperties;
import io.seata.spring.boot.autoconfigure.properties.client.LogProperties;
import io.seata.spring.boot.autoconfigure.properties.client.RmProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ServiceProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ShutdownProperties;
import io.seata.spring.boot.autoconfigure.properties.client.ThreadFactoryProperties;
import io.seata.spring.boot.autoconfigure.properties.client.TmProperties;
import io.seata.spring.boot.autoconfigure.properties.client.TransportProperties;
import io.seata.spring.boot.autoconfigure.properties.client.UndoProperties;
import io.seata.spring.boot.autoconfigure.properties.client.UndoCompressProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigApolloProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigConsulProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigCustomProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigEtcd3Properties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigFileProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigNacosProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigProperties;
import io.seata.spring.boot.autoconfigure.properties.config.ConfigZooKeeperProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryConsulProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryCustomProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryEtcd3Properties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryEurekaProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryNacosProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryRedisProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistrySofaProperties;
import io.seata.spring.boot.autoconfigure.properties.registry.RegistryZooKeeperProperties;
import io.seata.spring.boot.autoconfigure.provider.SpringApplicationContextProvider;
import io.seata.tm.api.DefaultFailureHandlerImpl;
import io.seata.tm.api.FailureHandler;
......@@ -63,35 +36,7 @@ import static io.seata.common.Constants.BEAN_NAME_FAILURE_HANDLER;
import static io.seata.common.Constants.BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER;
import static io.seata.spring.annotation.datasource.AutoDataSourceProxyRegistrar.BEAN_NAME_SEATA_AUTO_DATA_SOURCE_PROXY_CREATOR;
import static io.seata.spring.annotation.datasource.AutoDataSourceProxyRegistrar.BEAN_NAME_SEATA_DATA_SOURCE_BEAN_POST_PROCESSOR;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_RM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_TM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_APOLLO_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_CONSUL_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_CUSTOM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_ETCD3_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_FILE_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_NACOS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_ZK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.LOCK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.LOG_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.PROPERTY_BEAN_MAP;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_CONSUL_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_CUSTOM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_ETCD3_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_EUREKA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_NACOS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_REDIS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_SOFA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_ZK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SEATA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SERVICE_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SHUTDOWN_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.THREAD_FACTORY_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.TRANSPORT_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.UNDO_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.COMPRESS_PREFIX;
/**
* @author xingfudeshi@gmail.com
......@@ -103,47 +48,10 @@ import static io.seata.spring.boot.autoconfigure.StarterConstants.COMPRESS_PREFI
public class SeataAutoConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(SeataAutoConfiguration.class);
public SeataAutoConfiguration(SeataProperties seataProperties,
RmProperties rmProperties, TmProperties tmProperties, LockProperties lockProperties,
ServiceProperties serviceProperties, ShutdownProperties shutdownProperties, ThreadFactoryProperties threadFactoryProperties,
UndoProperties undoProperties, UndoCompressProperties undoCompressProperties, LogProperties logProperties, TransportProperties transportProperties,
ConfigProperties configProperties, ConfigFileProperties configFileProperties, RegistryProperties registryProperties,
ConfigNacosProperties configNacosProperties, ConfigConsulProperties configConsulProperties, ConfigZooKeeperProperties configZooKeeperProperties,
ConfigApolloProperties configApolloProperties, ConfigEtcd3Properties configEtcd3Properties, ConfigCustomProperties configCustomProperties,
RegistryConsulProperties registryConsulProperties, RegistryEtcd3Properties registryEtcd3Properties, RegistryEurekaProperties registryEurekaProperties,
RegistryNacosProperties registryNacosProperties, RegistryRedisProperties registryRedisProperties, RegistrySofaProperties registrySofaProperties,
RegistryZooKeeperProperties registryZooKeeperProperties, RegistryCustomProperties registryCustomProperties) {
PROPERTY_BEAN_MAP.put(SEATA_PREFIX, seataProperties);
PROPERTY_BEAN_MAP.put(CLIENT_RM_PREFIX, rmProperties);
PROPERTY_BEAN_MAP.put(CLIENT_TM_PREFIX, tmProperties);
PROPERTY_BEAN_MAP.put(LOCK_PREFIX, lockProperties);
PROPERTY_BEAN_MAP.put(SERVICE_PREFIX, serviceProperties);
PROPERTY_BEAN_MAP.put(SHUTDOWN_PREFIX, shutdownProperties);
PROPERTY_BEAN_MAP.put(THREAD_FACTORY_PREFIX, threadFactoryProperties);
PROPERTY_BEAN_MAP.put(UNDO_PREFIX, undoProperties);
PROPERTY_BEAN_MAP.put(COMPRESS_PREFIX, undoCompressProperties);
PROPERTY_BEAN_MAP.put(LOG_PREFIX, logProperties);
PROPERTY_BEAN_MAP.put(TRANSPORT_PREFIX, transportProperties);
PROPERTY_BEAN_MAP.put(CONFIG_PREFIX, configProperties);
PROPERTY_BEAN_MAP.put(CONFIG_FILE_PREFIX, configFileProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_PREFIX, registryProperties);
PROPERTY_BEAN_MAP.put(CONFIG_NACOS_PREFIX, configNacosProperties);
PROPERTY_BEAN_MAP.put(CONFIG_CONSUL_PREFIX, configConsulProperties);
PROPERTY_BEAN_MAP.put(CONFIG_ZK_PREFIX, configZooKeeperProperties);
PROPERTY_BEAN_MAP.put(CONFIG_APOLLO_PREFIX, configApolloProperties);
PROPERTY_BEAN_MAP.put(CONFIG_ETCD3_PREFIX, configEtcd3Properties);
PROPERTY_BEAN_MAP.put(CONFIG_CUSTOM_PREFIX, configCustomProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_CONSUL_PREFIX, registryConsulProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_ETCD3_PREFIX, registryEtcd3Properties);
PROPERTY_BEAN_MAP.put(REGISTRY_EUREKA_PREFIX, registryEurekaProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_NACOS_PREFIX, registryNacosProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_REDIS_PREFIX, registryRedisProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_SOFA_PREFIX, registrySofaProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_ZK_PREFIX, registryZooKeeperProperties);
PROPERTY_BEAN_MAP.put(REGISTRY_CUSTOM_PREFIX, registryCustomProperties);
@Bean
@ConditionalOnMissingBean(PropertyBeanPostProcessor.class)
public PropertyBeanPostProcessor propertyBeanPostProcessor() {
return new PropertyBeanPostProcessor();
}
@Bean(BEAN_NAME_SPRING_APPLICATION_CONTEXT_PROVIDER)
......@@ -191,7 +99,7 @@ public class SeataAutoConfiguration {
@ConditionalOnMissingBean(SeataAutoDataSourceProxyCreator.class)
public SeataAutoDataSourceProxyCreator seataAutoDataSourceProxyCreator(SeataProperties seataProperties) {
return new SeataAutoDataSourceProxyCreator(seataProperties.isUseJdkProxy(),
seataProperties.getExcludesForAutoProxying(), seataProperties.getDataSourceProxyMode());
seataProperties.getExcludesForAutoProxying(), seataProperties.getDataSourceProxyMode());
}
}
}
......@@ -16,6 +16,7 @@
package io.seata.spring.boot.autoconfigure;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
/**
* @author xingfudeshi@gmail.com
......@@ -56,7 +57,7 @@ public interface StarterConstants {
String CONFIG_CUSTOM_PREFIX = CONFIG_PREFIX + ".custom";
int MAP_CAPACITY = 64;
HashMap<String, Object> PROPERTY_BEAN_MAP = new HashMap<>(MAP_CAPACITY);
HashMap<String, CompletableFuture<Object>> PROPERTY_BEAN_MAP = new HashMap<>(MAP_CAPACITY);
/**
* The following special keys need to be normalized.
......
......@@ -19,6 +19,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import io.seata.config.Configuration;
......@@ -31,11 +35,37 @@ import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import static io.seata.common.util.StringFormatUtils.DOT;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_RM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CLIENT_TM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.COMPRESS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_APOLLO_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_CONSUL_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_CUSTOM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_ETCD3_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_FILE_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_NACOS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.CONFIG_ZK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.LOCK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.LOG_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.PROPERTY_BEAN_MAP;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_CONSUL_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_CUSTOM_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_ETCD3_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_EUREKA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_NACOS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_REDIS_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_SOFA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_ZK_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SEATA_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SERVICE_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SHUTDOWN_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SPECIAL_KEY_GROUPLIST;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SPECIAL_KEY_VGROUP_MAPPING;
import static io.seata.spring.boot.autoconfigure.StarterConstants.THREAD_FACTORY_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.TRANSPORT_PREFIX;
import static io.seata.spring.boot.autoconfigure.StarterConstants.UNDO_PREFIX;
/**
* @author xingfudeshi@gmail.com
......@@ -44,6 +74,41 @@ public class SpringBootConfigurationProvider implements ExtConfigurationProvider
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootConfigurationProvider.class);
private static final String INTERCEPT_METHOD_PREFIX = "get";
static {
PROPERTY_BEAN_MAP.putIfAbsent(SEATA_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CLIENT_RM_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CLIENT_TM_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(LOCK_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(SERVICE_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(SHUTDOWN_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(THREAD_FACTORY_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(UNDO_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(COMPRESS_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(LOG_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(TRANSPORT_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_FILE_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_NACOS_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_CONSUL_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_ZK_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_APOLLO_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_ETCD3_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(CONFIG_CUSTOM_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_CONSUL_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_ETCD3_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_EUREKA_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_NACOS_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_REDIS_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_SOFA_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_ZK_PREFIX, new CompletableFuture<>());
PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_CUSTOM_PREFIX, new CompletableFuture<>());
}
@Override
public Configuration provide(Configuration originalConfiguration) {
return (Configuration) Enhancer.create(originalConfiguration.getClass(), new MethodInterceptor() {
......@@ -74,12 +139,12 @@ public class SpringBootConfigurationProvider implements ExtConfigurationProvider
});
}
private Object get(String dataId, Object defaultValue, long timeoutMills) throws IllegalAccessException {
private Object get(String dataId, Object defaultValue, long timeoutMills) throws IllegalAccessException, ExecutionException, InterruptedException, TimeoutException {
return get(dataId, defaultValue);
}
private Object get(String dataId, Object defaultValue) throws IllegalAccessException {
private Object get(String dataId, Object defaultValue) throws IllegalAccessException, ExecutionException, InterruptedException, TimeoutException {
Object result = get(dataId);
if (result == null) {
return defaultValue;
......@@ -87,10 +152,11 @@ public class SpringBootConfigurationProvider implements ExtConfigurationProvider
return result;
}
private Object get(String dataId) throws IllegalAccessException {
private Object get(String dataId) throws IllegalAccessException, ExecutionException, InterruptedException, TimeoutException {
String propertyPrefix = getPropertyPrefix(dataId);
Object propertyObject = PROPERTY_BEAN_MAP.get(propertyPrefix);
if (propertyObject != null) {
CompletableFuture<Object> propertyFuture = PROPERTY_BEAN_MAP.get(propertyPrefix);
if (propertyFuture != null) {
Object propertyObject = propertyFuture.get(10000, TimeUnit.MILLISECONDS);
String propertySuffix = getPropertySuffix(dataId);
Optional<Field> fieldOptional = Stream.of(propertyObject.getClass().getDeclaredFields()).filter(
f -> f.getName().equalsIgnoreCase(propertySuffix)).findAny();
......@@ -107,7 +173,7 @@ public class SpringBootConfigurationProvider implements ExtConfigurationProvider
} else {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Property bean with prefix '{}' was not found in `StarterConstants.PROPERTY_BEAN_MAP`."
+ " Please inform the {} committer to fix this BUG.", propertyPrefix, SEATA_PREFIX);
+ " Please inform the {} committer to fix this BUG.", propertyPrefix, SEATA_PREFIX);
}
}
return null;
......
package io.seata.spring.boot.autoconfigure;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import io.seata.spring.boot.autoconfigure.properties.SpringCloudAlibabaConfiguration;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static io.seata.spring.boot.autoconfigure.StarterConstants.PROPERTY_BEAN_MAP;
import static io.seata.spring.boot.autoconfigure.StarterConstants.SEATA_PREFIX;
/**
* @author xingfudeshi@gmail.com
*/
@Configuration
public class PropertyBeanPostProcessorTest {
private static AnnotationConfigApplicationContext context;
@BeforeAll
public static void initContext() {
PROPERTY_BEAN_MAP.putIfAbsent(SEATA_PREFIX, new CompletableFuture<>());
context = new AnnotationConfigApplicationContext(PropertyBeanPostProcessorTest.class);
}
@Bean
public PropertyBeanPostProcessor propertyBeanPostProcessor() {
return new PropertyBeanPostProcessor();
}
@Bean
public SeataProperties seataProperties() {
SeataProperties seataProperties = new SeataProperties();
seataProperties.setApplicationId("test-id");
return seataProperties;
}
@Bean
public SpringCloudAlibabaConfiguration springCloudAlibabaConfiguration() {
return new SpringCloudAlibabaConfiguration();
}
@Test
public void testCompletePropertyBean() throws ExecutionException, InterruptedException, TimeoutException {
Object object = PROPERTY_BEAN_MAP.get(SEATA_PREFIX).get(3, TimeUnit.SECONDS);
Assertions.assertThat(object).isInstanceOf(SeataProperties.class);
SeataProperties seataProperties = (SeataProperties) object;
Assertions.assertThat(seataProperties.getApplicationId()).isEqualTo("test-id");
}
@AfterAll
public static void closeContext() {
context.close();
}
}
......@@ -15,6 +15,8 @@
*/
package io.seata.spring.boot.autoconfigure;
import java.util.concurrent.CompletableFuture;
import io.seata.common.loader.EnhancedServiceLoader;
import io.seata.config.Configuration;
import io.seata.config.ExtConfigurationProvider;
......@@ -49,7 +51,10 @@ public class RedisAutoInjectionTypeConvertTest {
@Bean
RegistryRedisProperties registryRedisProperties() {
RegistryRedisProperties registryRedisProperties = new RegistryRedisProperties().setPassword("123456").setDb(1).setServerAddr("localhost:123456");
PROPERTY_BEAN_MAP.put(REGISTRY_REDIS_PREFIX, registryRedisProperties);
CompletableFuture<Object> completableFuture = new CompletableFuture<>();
if (PROPERTY_BEAN_MAP.putIfAbsent(REGISTRY_REDIS_PREFIX, completableFuture) == null) {
completableFuture.complete(registryRedisProperties);
}
return registryRedisProperties;
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment