Skip to content

Commit 49768e2

Browse files
committed
Register config classes once in reactive child context
Fixes gh-10939
1 parent 1f47672 commit 49768e2

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.boot.actuate.autoconfigure.web.reactive;
1818

1919
import java.lang.reflect.Modifier;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.List;
2023

2124
import org.springframework.beans.FatalBeanException;
2225
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -42,8 +45,9 @@ public ConfigurableApplicationContext createManagementContext(
4245
ApplicationContext parent, Class<?>... configClasses) {
4346
AnnotationConfigReactiveWebServerApplicationContext child = new AnnotationConfigReactiveWebServerApplicationContext();
4447
child.setParent(parent);
45-
child.register(configClasses);
46-
child.register(ReactiveWebServerAutoConfiguration.class);
48+
List<Class<?>> combinedClasses = new ArrayList<>(Arrays.asList(configClasses));
49+
combinedClasses.add(ReactiveWebServerAutoConfiguration.class);
50+
child.register(combinedClasses.toArray(new Class<?>[combinedClasses.size()]));
4751
registerReactiveWebServerFactory(parent, child);
4852
return child;
4953
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2012-2017 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+
* http://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.boot.actuate.autoconfigure.web.reactive;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration;
22+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
23+
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.http.server.reactive.HttpHandler;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
32+
/**
33+
* Tests for {@link ReactiveManagementContextFactory}.
34+
*
35+
* @author Madhura Bhave
36+
*/
37+
public class ReactiveManagementContextFactoryTests {
38+
39+
private ReactiveManagementContextFactory factory = new ReactiveManagementContextFactory();
40+
41+
private AnnotationConfigReactiveWebServerApplicationContext parent = new AnnotationConfigReactiveWebServerApplicationContext();
42+
43+
@Test
44+
public void createManagementContextShouldCreateChildContextWithConfigClasses() throws Exception {
45+
this.parent.register(ParentConfiguration.class);
46+
this.parent.refresh();
47+
AnnotationConfigReactiveWebServerApplicationContext childContext = (AnnotationConfigReactiveWebServerApplicationContext) this.factory.createManagementContext(this.parent,
48+
TestConfiguration1.class, TestConfiguration2.class);
49+
childContext.refresh();
50+
assertThat(childContext.getBean(TestConfiguration1.class)).isNotNull();
51+
assertThat(childContext.getBean(TestConfiguration2.class)).isNotNull();
52+
assertThat(childContext.getBean(ReactiveWebServerAutoConfiguration.class)).isNotNull();
53+
}
54+
55+
@Configuration
56+
static class ParentConfiguration {
57+
58+
@Bean
59+
public ReactiveWebServerFactory reactiveWebServerFactory() {
60+
return mock(ReactiveWebServerFactory.class);
61+
}
62+
63+
@Bean
64+
public HttpHandler httpHandler(ApplicationContext applicationContext) {
65+
return mock(HttpHandler.class);
66+
}
67+
68+
}
69+
70+
@Configuration
71+
static class TestConfiguration1 {
72+
73+
@Bean
74+
public HttpHandler httpHandler(ApplicationContext applicationContext) {
75+
return mock(HttpHandler.class);
76+
}
77+
78+
}
79+
80+
@Configuration
81+
static class TestConfiguration2 {
82+
83+
}
84+
85+
}

0 commit comments

Comments
 (0)