Skip to content

Commit ab2b04f

Browse files
committed
Find health contributors in ancestor contexts in non-reactive apps
Previously, health contributors in a non-reative app were found by retrieving them from the application context rather than via dependency injection. This results in only contributors from the current context being found, with contributors in ancestor contexts ignored. This commit moves to injection of the contributors, aligning the behaviour with that of a reactive application. Closes gh-27308
1 parent 5afa04f commit ab2b04f

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ HealthEndpointGroups healthEndpointGroups(ApplicationContext applicationContext,
7777
@Bean
7878
@ConditionalOnMissingBean
7979
HealthContributorRegistry healthContributorRegistry(ApplicationContext applicationContext,
80-
HealthEndpointGroups groups) {
81-
Map<String, HealthContributor> healthContributors = new LinkedHashMap<>(
82-
applicationContext.getBeansOfType(HealthContributor.class));
80+
HealthEndpointGroups groups, Map<String, HealthContributor> healthContributors,
81+
Map<String, ReactiveHealthContributor> reactiveHealthContributors) {
8382
if (ClassUtils.isPresent("reactor.core.publisher.Flux", applicationContext.getClassLoader())) {
84-
healthContributors.putAll(new AdaptedReactiveHealthContributors(applicationContext).get());
83+
healthContributors.putAll(new AdaptedReactiveHealthContributors(reactiveHealthContributors).get());
8584
}
8685
return new AutoConfiguredHealthContributorRegistry(healthContributors, groups.getNames());
8786
}
@@ -137,10 +136,9 @@ private static class AdaptedReactiveHealthContributors {
137136

138137
private final Map<String, HealthContributor> adapted;
139138

140-
AdaptedReactiveHealthContributors(ApplicationContext applicationContext) {
139+
AdaptedReactiveHealthContributors(Map<String, ReactiveHealthContributor> reactiveContributors) {
141140
Map<String, HealthContributor> adapted = new LinkedHashMap<>();
142-
applicationContext.getBeansOfType(ReactiveHealthContributor.class)
143-
.forEach((name, contributor) -> adapted.put(name, adapt(contributor)));
141+
reactiveContributors.forEach((name, contributor) -> adapted.put(name, adapt(contributor)));
144142
this.adapted = Collections.unmodifiableMap(adapted);
145143
}
146144

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/health/HealthEndpointAutoConfigurationTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.actuate.autoconfigure.health;
1818

1919
import java.util.Collections;
20+
import java.util.Map;
2021

2122
import org.junit.jupiter.api.Test;
2223
import reactor.core.publisher.Flux;
@@ -45,8 +46,10 @@
4546
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
4647
import org.springframework.boot.actuate.health.Status;
4748
import org.springframework.boot.actuate.health.StatusAggregator;
49+
import org.springframework.boot.actuate.health.SystemHealth;
4850
import org.springframework.boot.autoconfigure.AutoConfigurations;
4951
import org.springframework.boot.test.context.FilteredClassLoader;
52+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
5053
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
5154
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
5255
import org.springframework.context.annotation.Bean;
@@ -264,6 +267,32 @@ void runWhenHasHealthEndpointGroupsPostProcessorPerformsProcessing() {
264267
});
265268
}
266269

270+
@Test
271+
void runWithIndicatorsInParentContextFindsIndicators() {
272+
new ApplicationContextRunner().withUserConfiguration(HealthIndicatorsConfiguration.class)
273+
.run((parent) -> new WebApplicationContextRunner().withConfiguration(AutoConfigurations
274+
.of(HealthContributorAutoConfiguration.class, HealthEndpointAutoConfiguration.class))
275+
.withParent(parent).run((context) -> {
276+
HealthComponent health = context.getBean(HealthEndpoint.class).health();
277+
Map<String, HealthComponent> components = ((SystemHealth) health).getComponents();
278+
assertThat(components).containsKeys("additional", "ping", "simple");
279+
}));
280+
}
281+
282+
@Test
283+
void runWithReactiveContextAndIndicatorsInParentContextFindsIndicators() {
284+
new ApplicationContextRunner().withUserConfiguration(HealthIndicatorsConfiguration.class)
285+
.run((parent) -> new ReactiveWebApplicationContextRunner()
286+
.withConfiguration(AutoConfigurations.of(HealthContributorAutoConfiguration.class,
287+
HealthEndpointAutoConfiguration.class, WebEndpointAutoConfiguration.class,
288+
EndpointAutoConfiguration.class))
289+
.withParent(parent).run((context) -> {
290+
HealthComponent health = context.getBean(HealthEndpoint.class).health();
291+
Map<String, HealthComponent> components = ((SystemHealth) health).getComponents();
292+
assertThat(components).containsKeys("additional", "ping", "simple");
293+
}));
294+
}
295+
267296
@Configuration(proxyBeanMethods = false)
268297
static class HealthIndicatorsConfiguration {
269298

0 commit comments

Comments
 (0)