Skip to content

Commit 5b8ae3a

Browse files
committed
Make sure bootstrap sources are ordered before use.
Make call to sort() before setting on builder.sources(). fixes spring-projectsgh-176 (cherry picked from commit 6dc35d1)
1 parent 710080c commit 5b8ae3a

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ private ConfigurableApplicationContext bootstrapServiceContext(
133133
}
134134
sources.add(cls);
135135
}
136-
builder.sources(sources.toArray(new Class[sources.size()]));
137136
AnnotationAwareOrderComparator.sort(sources);
137+
builder.sources(sources.toArray(new Class[sources.size()]));
138138
final ConfigurableApplicationContext context = builder.run();
139139
// Make the bootstrap context a parent of the app context
140140
addAncestorInitializer(application, context);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.cloud.bootstrap;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.cloud.bootstrap.BootstrapOrderingSpringApplicationJsonIntegrationTests.Application;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.env.ConfigurableEnvironment;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration.firstToBeCreated;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest(classes = Application.class)
18+
public class BootstrapSourcesOrderingTests {
19+
20+
@Autowired
21+
private ConfigurableEnvironment environment;
22+
23+
@Test
24+
public void sourcesAreOrderedCorrectly() {
25+
Class<?> firstConstructedClass = firstToBeCreated.get();
26+
assertThat(firstConstructedClass).as("bootstrap sources not ordered correctly").isEqualTo(TestHigherPriorityBootstrapConfiguration.class);
27+
}
28+
29+
@EnableAutoConfiguration
30+
@Configuration
31+
protected static class Application {
32+
}
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.cloud.bootstrap;
2+
3+
import org.springframework.core.annotation.Order;
4+
5+
import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration.firstToBeCreated;
6+
7+
/**
8+
* @author Spencer Gibb
9+
*/
10+
@Order(0)
11+
public class TestBootstrapConfiguration {
12+
13+
public TestBootstrapConfiguration() {
14+
firstToBeCreated.compareAndSet(null, TestBootstrapConfiguration.class);
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.springframework.cloud.bootstrap;
2+
3+
import java.util.concurrent.atomic.AtomicReference;
4+
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.annotation.Order;
7+
8+
/**
9+
* @author Spencer Gibb
10+
*/
11+
@Order(Ordered.HIGHEST_PRECEDENCE)
12+
public class TestHigherPriorityBootstrapConfiguration {
13+
14+
static final AtomicReference<Class<?>> firstToBeCreated = new AtomicReference<>();
15+
16+
public TestHigherPriorityBootstrapConfiguration() {
17+
firstToBeCreated.compareAndSet(null, TestHigherPriorityBootstrapConfiguration.class);
18+
}
19+
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Bootstrap components
2+
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
3+
org.springframework.cloud.bootstrap.TestBootstrapConfiguration,\
4+
org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration

0 commit comments

Comments
 (0)