From 217326b4d80e954d76ddf4a088371695486eec63 Mon Sep 17 00:00:00 2001 From: "zhijie.zheng" Date: Mon, 31 May 2021 13:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8=E5=A4=9A=E4=B8=AA/?= =?UTF-8?q?=E5=A4=9A=E7=BA=A7SpringContext=E5=B9=B6=E5=AD=98=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E4=B8=8B,EventPublishingConfigService=E5=8F=91?= =?UTF-8?q?=E9=80=81=E4=BA=8B=E4=BB=B6=E6=B7=B7=E4=B9=B1=EF=BC=8C=E5=BC=95?= =?UTF-8?q?=E5=8F=91=E5=90=8E=E7=BB=AD=E4=BE=9D=E8=B5=96=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E7=9A=84=E7=B3=BB=E5=88=97=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=A6=82@NacosValue=E6=B3=A8=E8=A7=A3=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=A7=A3=E6=9E=90=E5=88=B0=E6=9C=80=E6=96=B0=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...bleEventPublishingNacosServiceFactory.java | 19 ++++++-------- .../nacos/spring/util/NacosBeanUtils.java | 25 ++++++++----------- .../NacosPropertySourcePostProcessorTest.java | 7 +++++- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/factory/CacheableEventPublishingNacosServiceFactory.java b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/factory/CacheableEventPublishingNacosServiceFactory.java index ea5e6ef3..68f296b1 100644 --- a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/factory/CacheableEventPublishingNacosServiceFactory.java +++ b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/factory/CacheableEventPublishingNacosServiceFactory.java @@ -28,8 +28,10 @@ import java.util.Properties; import java.util.concurrent.ExecutorService; +import com.alibaba.nacos.spring.util.NacosUtils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import com.alibaba.nacos.api.NacosFactory; @@ -46,9 +48,7 @@ * @since 0.1.0 */ @SuppressWarnings("unchecked") -public class CacheableEventPublishingNacosServiceFactory implements NacosServiceFactory { - - private static volatile CacheableEventPublishingNacosServiceFactory SINGLETON = new CacheableEventPublishingNacosServiceFactory(); +public class CacheableEventPublishingNacosServiceFactory implements NacosServiceFactory, ApplicationContextAware { private final Map configServicesCache = new LinkedHashMap( 2); @@ -75,10 +75,6 @@ public CacheableEventPublishingNacosServiceFactory() { createWorkerManager = Collections.unmodifiableMap(createWorkerManager); } - public static CacheableEventPublishingNacosServiceFactory getSingleton() { - return SINGLETON; - } - @Override public ConfigService createConfigService(Properties properties) throws NacosException { @@ -146,9 +142,9 @@ else if (o instanceof NamingMaintainService) { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = (ConfigurableApplicationContext) applicationContext; - this.nacosConfigListenerExecutor = getSingleton().nacosConfigListenerExecutor == null + this.nacosConfigListenerExecutor = this.nacosConfigListenerExecutor == null ? getNacosConfigListenerExecutorIfPresent(applicationContext) - : getSingleton().nacosConfigListenerExecutor; + : this.nacosConfigListenerExecutor; } @Override @@ -243,6 +239,7 @@ class ConfigCreateWorker extends AbstractCreateWorker { public ConfigService run(Properties properties, ConfigService service) throws NacosException { String cacheKey = identify(properties); + cacheKey += NacosUtils.SEPARATOR + context.getId() + NacosUtils.SEPARATOR; ConfigService configService = configServicesCache.get(cacheKey); if (configService == null) { @@ -250,8 +247,8 @@ public ConfigService run(Properties properties, ConfigService service) service = NacosFactory.createConfigService(properties); } configService = new EventPublishingConfigService(service, properties, - getSingleton().context, - getSingleton().nacosConfigListenerExecutor); + CacheableEventPublishingNacosServiceFactory.this.context, + CacheableEventPublishingNacosServiceFactory.this.nacosConfigListenerExecutor); configServicesCache.put(cacheKey, configService); } return configService; diff --git a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/NacosBeanUtils.java b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/NacosBeanUtils.java index 9eeac6bd..729fda67 100644 --- a/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/NacosBeanUtils.java +++ b/nacos-spring-context/src/main/java/com/alibaba/nacos/spring/util/NacosBeanUtils.java @@ -401,6 +401,14 @@ public static void registerNacosConfigBeans(BeanDefinitionRegistry registry, registerConfigServiceBeanBuilder(registry); registerLoggingNacosConfigMetadataEventListener(registry); + + registerCacheableEventPublishingNacosServiceFactory(registry,beanFactory); + } + + private static void registerCacheableEventPublishingNacosServiceFactory(BeanDefinitionRegistry registry, BeanFactory beanFactory) { + registerInfrastructureBeanIfAbsent(registry, + NacosServiceFactory.BEAN_NAME, + CacheableEventPublishingNacosServiceFactory.class); } /** @@ -491,21 +499,8 @@ public static Properties getGlobalPropertiesBean(BeanFactory beanFactory) */ public static NacosServiceFactory getNacosServiceFactoryBean(BeanFactory beanFactory) throws NoSuchBeanDefinitionException { - if (null == beanFactory) { - return getNacosServiceFactoryBean(); - } - ApplicationContextHolder applicationContextHolder = getApplicationContextHolder( - beanFactory); - CacheableEventPublishingNacosServiceFactory nacosServiceFactory = CacheableEventPublishingNacosServiceFactory - .getSingleton(); - nacosServiceFactory - .setApplicationContext(applicationContextHolder.getApplicationContext()); - return nacosServiceFactory; - } - - public static NacosServiceFactory getNacosServiceFactoryBean() - throws NoSuchBeanDefinitionException { - return CacheableEventPublishingNacosServiceFactory.getSingleton(); + return beanFactory.getBean(NacosServiceFactory.BEAN_NAME, + NacosServiceFactory.class); } public static ApplicationContextHolder getApplicationContextHolder( diff --git a/nacos-spring-context/src/test/java/com/alibaba/nacos/spring/context/annotation/config/NacosPropertySourcePostProcessorTest.java b/nacos-spring-context/src/test/java/com/alibaba/nacos/spring/context/annotation/config/NacosPropertySourcePostProcessorTest.java index 0f99b5cd..af66b43c 100644 --- a/nacos-spring-context/src/test/java/com/alibaba/nacos/spring/context/annotation/config/NacosPropertySourcePostProcessorTest.java +++ b/nacos-spring-context/src/test/java/com/alibaba/nacos/spring/context/annotation/config/NacosPropertySourcePostProcessorTest.java @@ -28,11 +28,14 @@ import java.util.HashMap; import java.util.Map; +import com.alibaba.nacos.spring.factory.CacheableEventPublishingNacosServiceFactory; +import com.alibaba.nacos.spring.factory.NacosServiceFactory; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -84,6 +87,8 @@ public class NacosPropertySourcePostProcessorTest + "PATH = /My/Path"; @NacosInjected private ConfigService configService; + @Autowired + private NacosServiceFactory nacosServiceFactory; @Override public void init(EmbeddedNacosHttpServer httpServer) { @@ -179,7 +184,7 @@ private AnnotationConfigApplicationContext createContext(String dataId, configService.publishConfig(dataId, groupId, content); beanFactory.registerSingleton(CONFIG_SERVICE_BEAN_NAME, configService); - + beanFactory.registerSingleton(NacosServiceFactory.BEAN_NAME,nacosServiceFactory); context.register(AnnotationNacosInjectedBeanPostProcessor.class, NacosPropertySourcePostProcessor.class, ConfigServiceBeanBuilder.class, AnnotationNacosPropertySourceBuilder.class, TestConfiguration.class,