From a43cd30d239558fbab7129a89217fe50d5264219 Mon Sep 17 00:00:00 2001 From: yunlongn Date: Sat, 7 Oct 2023 09:38:00 +0800 Subject: [PATCH] [type:fix] applicationContextAware initialization too late . (#5192) * [type:fix] applicationContextAware initialization too late . * [type:fix] applicationContextAware initialization too late . * [type:fix] applicationContextAware initialization too late . * [type:fix] applicationContextAware initialization too late . --- .../springcloud/SpringCloudPluginTest.java | 2 +- .../configuration/SpringExtConfiguration.java | 29 ++++--------------- .../SpringExtConfigurationTest.java | 13 --------- 3 files changed, 7 insertions(+), 37 deletions(-) diff --git a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java index 19a3d840bd64..9edeb78c03cd 100644 --- a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java +++ b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/src/test/java/org/apache/shenyu/e2e/testcase/springcloud/SpringCloudPluginTest.java @@ -74,7 +74,7 @@ static void setup(final AdminClient adminClient, final GatewayClient gatewayClie List selectorDTOList = adminClient.listAllSelectors(); for (SelectorDTO selectorDTO : selectorDTOList) { - if (selectorDTO.getHandle() != null && !"".equals(selectorDTO.getHandle())) { + if (selectorDTO.getHandle() != null && !"{}".equals(selectorDTO.getHandle())) { SpringCloudPluginCases.verifierUri(selectorDTO.getHandle()); } } diff --git a/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/SpringExtConfiguration.java b/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/SpringExtConfiguration.java index fa1ce5412f49..fe27ddae7865 100644 --- a/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/SpringExtConfiguration.java +++ b/shenyu-web/src/main/java/org/apache/shenyu/web/configuration/SpringExtConfiguration.java @@ -23,7 +23,6 @@ import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.lang.NonNull; @@ -31,28 +30,12 @@ * The type spring ext configuration. */ @Configuration -public class SpringExtConfiguration { +public class SpringExtConfiguration implements ApplicationContextAware { - /** - * Application context aware application context aware. - * - * @return the application context aware - */ - @Bean - public ApplicationContextAware applicationContextAware() { - return new ShenyuApplicationContextAware(); - } - - /** - * The type shenyu application context aware. - */ - public static class ShenyuApplicationContextAware implements ApplicationContextAware { - - @Override - public void setApplicationContext(@NonNull final ApplicationContext applicationContext) throws BeansException { - SpringBeanUtils.getInstance().setApplicationContext(applicationContext); - ShenyuConfig shenyuConfig = SpringBeanUtils.getInstance().getBean(ShenyuConfig.class); - Singleton.INST.single(ShenyuConfig.class, shenyuConfig); - } + @Override + public void setApplicationContext(@NonNull final ApplicationContext applicationContext) throws BeansException { + SpringBeanUtils.getInstance().setApplicationContext(applicationContext); + ShenyuConfig shenyuConfig = SpringBeanUtils.getInstance().getBean(ShenyuConfig.class); + Singleton.INST.single(ShenyuConfig.class, shenyuConfig); } } diff --git a/shenyu-web/src/test/java/org/apache/shenyu/web/configuration/SpringExtConfigurationTest.java b/shenyu-web/src/test/java/org/apache/shenyu/web/configuration/SpringExtConfigurationTest.java index 72a9ae0117e1..62cdc017bd88 100644 --- a/shenyu-web/src/test/java/org/apache/shenyu/web/configuration/SpringExtConfigurationTest.java +++ b/shenyu-web/src/test/java/org/apache/shenyu/web/configuration/SpringExtConfigurationTest.java @@ -19,14 +19,11 @@ import org.apache.shenyu.common.config.ShenyuConfig; import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -37,22 +34,12 @@ @EnableConfigurationProperties public class SpringExtConfigurationTest { - private SpringExtConfiguration springExtConfiguration; - - @BeforeEach - public void before() { - springExtConfiguration = new SpringExtConfiguration(); - } - @Test public void applicationContextAwareTest() { - ApplicationContextAware applicationContextAware = springExtConfiguration.applicationContextAware(); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); SpringBeanUtils.getInstance().setApplicationContext(applicationContext); when(SpringBeanUtils.getInstance().getBean(ShenyuConfig.class)) .thenReturn(new ShenyuConfig()); - applicationContextAware.setApplicationContext(applicationContext); - assertNotNull(applicationContextAware); } }