Skip to content

Commit ec871d6

Browse files
committed
Fix StatusAggregator static initialization
Prior to this commit, there was a cycle between `StatusAggregator` and `SimpleStatusAggregator`, which caused a static initialization bug - depending on which class (the implementation or its interface) was loaded first. This commit turns the static field of the `StatusAggregator` interface into a static method to avoid this problem. Fixes gh-21211
1 parent ea5f282 commit ec871d6

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesHealthEndpointGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public boolean showDetails(SecurityContext securityContext) {
5656

5757
@Override
5858
public StatusAggregator getStatusAggregator() {
59-
return StatusAggregator.DEFAULT;
59+
return StatusAggregator.getDefault();
6060
}
6161

6262
@Override

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/availability/AvailabilityProbesHealthEndpointGroupTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void showDetailsReturnsFalse() {
5757

5858
@Test
5959
void getStatusAggregatorReturnsDefaultStatusAggregator() {
60-
assertThat(this.group.getStatusAggregator()).isEqualTo(StatusAggregator.DEFAULT);
60+
assertThat(this.group.getStatusAggregator()).isEqualTo(StatusAggregator.getDefault());
6161
}
6262

6363
@Test

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -37,13 +37,17 @@
3737
public class SimpleStatusAggregator implements StatusAggregator {
3838

3939
private static final List<String> DEFAULT_ORDER;
40+
41+
static final StatusAggregator INSTANCE;
42+
4043
static {
4144
List<String> defaultOrder = new ArrayList<>();
4245
defaultOrder.add(Status.DOWN.getCode());
4346
defaultOrder.add(Status.OUT_OF_SERVICE.getCode());
4447
defaultOrder.add(Status.UP.getCode());
4548
defaultOrder.add(Status.UNKNOWN.getCode());
4649
DEFAULT_ORDER = Collections.unmodifiableList(getUniformCodes(defaultOrder.stream()));
50+
INSTANCE = new SimpleStatusAggregator();
4751
}
4852

4953
private final List<String> order;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
public interface StatusAggregator {
3434

3535
/**
36-
* A {@link StatusAggregator} instance using default ordering rules.
36+
* Return {@link StatusAggregator} instance using default ordering rules.
37+
* @return a {@code StatusAggregator} with default ordering rules.
3738
* @since 2.3.0
3839
*/
39-
StatusAggregator DEFAULT = new SimpleStatusAggregator();
40+
static StatusAggregator getDefault() {
41+
return SimpleStatusAggregator.INSTANCE;
42+
}
4043

4144
/**
4245
* Return the aggregate status for the given set of statuses.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -29,7 +29,14 @@
2929
class SimpleStatusAggregatorTests {
3030

3131
@Test
32-
void getAggregateStatusWhenUsingDefaultOrder() {
32+
void getAggregateStatusWhenUsingDefaultInstance() {
33+
StatusAggregator aggregator = StatusAggregator.getDefault();
34+
Status status = aggregator.getAggregateStatus(Status.DOWN, Status.UP, Status.UNKNOWN, Status.OUT_OF_SERVICE);
35+
assertThat(status).isEqualTo(Status.DOWN);
36+
}
37+
38+
@Test
39+
void getAggregateStatusWhenUsingNewDefaultOrder() {
3340
SimpleStatusAggregator aggregator = new SimpleStatusAggregator();
3441
Status status = aggregator.getAggregateStatus(Status.DOWN, Status.UP, Status.UNKNOWN, Status.OUT_OF_SERVICE);
3542
assertThat(status).isEqualTo(Status.DOWN);

0 commit comments

Comments
 (0)