Skip to content

Commit 53fc3b2

Browse files
committed
fix issue
Signed-off-by: wind57 <[email protected]>
1 parent fe28576 commit 53fc3b2

File tree

3 files changed

+89
-4
lines changed
  • spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config
  • spring-cloud-kubernetes-fabric8-discovery

3 files changed

+89
-4
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapCont
339339
String name, ApplicationListener<?> listener) {
340340
bootstrapContext.registerIfAbsent(cls, BootstrapRegistry.InstanceSupplier.of(instance));
341341
bootstrapContext.addCloseListener(event -> {
342-
if (event.getApplicationContext().getBeanFactory().getSingleton(name) == null) {
343-
event.getApplicationContext()
344-
.getBeanFactory()
345-
.registerSingleton(name, event.getBootstrapContext().get(cls));
342+
343+
T singleton = event.getBootstrapContext().get(cls);
344+
345+
if (event.getApplicationContext().getBeanFactory().getSingleton(name) == null && singleton != null) {
346+
event.getApplicationContext().getBeanFactory().registerSingleton(name, singleton);
346347
event.getApplicationContext().addApplicationListener(listener);
347348
}
348349
});

spring-cloud-kubernetes-fabric8-discovery/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
</dependency>
5050

5151
<!-- Testing Dependencies -->
52+
53+
<dependency>
54+
<groupId>org.springframework.cloud</groupId>
55+
<artifactId>spring-cloud-kubernetes-fabric8-config</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
5259
<dependency>
5360
<groupId>org.springframework.boot</groupId>
5461
<artifactId>spring-boot-starter-test</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.discovery;
18+
19+
import com.github.tomakehurst.wiremock.WireMockServer;
20+
import com.github.tomakehurst.wiremock.client.WireMock;
21+
import org.assertj.core.api.Assertions;
22+
import org.junit.jupiter.api.AfterAll;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.context.ApplicationContext;
30+
31+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
32+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
33+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
34+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
35+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
36+
37+
/**
38+
* Test that proves that this
39+
* <a href="https://github.com/spring-cloud/spring-cloud-kubernetes/issues/1831">issue</a>
40+
* is fixed.
41+
*
42+
* @author wind57
43+
*/
44+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
45+
"spring.main.cloud-platform=KUBERNETES", "spring.config.import=kubernetes:, optional:configserver:" })
46+
class Fabric8ConfigServerTest {
47+
48+
private static WireMockServer wireMockServer;
49+
50+
@Autowired
51+
private ApplicationContext applicationContext;
52+
53+
@BeforeAll
54+
static void beforeAll() {
55+
wireMockServer = new WireMockServer(options().port(8888));
56+
wireMockServer.start();
57+
WireMock.configureFor("localhost", wireMockServer.port());
58+
}
59+
60+
@AfterAll
61+
static void after() {
62+
WireMock.shutdownServer();
63+
wireMockServer.stop();
64+
}
65+
66+
@Test
67+
void test() {
68+
stubFor(get(urlEqualTo("/application/default")).willReturn(aResponse().withStatus(200).withBody("{}")));
69+
Assertions.assertThat(applicationContext).isNotNull();
70+
}
71+
72+
@SpringBootApplication
73+
protected static class TestConfig {
74+
75+
}
76+
77+
}

0 commit comments

Comments
 (0)