Skip to content

Commit

Permalink
[type:fix] applicationContextAware initialization too late . (apache#…
Browse files Browse the repository at this point in the history
…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 .
  • Loading branch information
yunlongn authored Oct 7, 2023
1 parent a9ca7fd commit a43cd30
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void setup(final AdminClient adminClient, final GatewayClient gatewayClie

List<SelectorDTO> 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,19 @@
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;

/**
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

}

0 comments on commit a43cd30

Please sign in to comment.