diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java index 8346c63e..76428941 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java @@ -133,8 +133,8 @@ public class BootstrapApplicationListener } sources.add(cls); } - builder.sources(sources.toArray(new Class[sources.size()])); AnnotationAwareOrderComparator.sort(sources); + builder.sources(sources.toArray(new Class[sources.size()])); final ConfigurableApplicationContext context = builder.run(); // Make the bootstrap context a parent of the app context addAncestorInitializer(application, context); diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapSourcesOrderingTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapSourcesOrderingTests.java new file mode 100644 index 00000000..84df3ad0 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapSourcesOrderingTests.java @@ -0,0 +1,34 @@ +package org.springframework.cloud.bootstrap; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.bootstrap.BootstrapOrderingSpringApplicationJsonIntegrationTests.Application; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration.firstToBeCreated; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class BootstrapSourcesOrderingTests { + + @Autowired + private ConfigurableEnvironment environment; + + @Test + public void sourcesAreOrderedCorrectly() { + Class firstConstructedClass = firstToBeCreated.get(); + assertThat(firstConstructedClass).as("bootstrap sources not ordered correctly").isEqualTo(TestHigherPriorityBootstrapConfiguration.class); + } + + @EnableAutoConfiguration + @Configuration + protected static class Application { + } + +} diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java new file mode 100644 index 00000000..798db7b8 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java @@ -0,0 +1,17 @@ +package org.springframework.cloud.bootstrap; + +import org.springframework.core.annotation.Order; + +import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration.firstToBeCreated; + +/** + * @author Spencer Gibb + */ +@Order(0) +public class TestBootstrapConfiguration { + + public TestBootstrapConfiguration() { + firstToBeCreated.compareAndSet(null, TestBootstrapConfiguration.class); + } + +} diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestHigherPriorityBootstrapConfiguration.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestHigherPriorityBootstrapConfiguration.java new file mode 100644 index 00000000..2494a5b6 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestHigherPriorityBootstrapConfiguration.java @@ -0,0 +1,20 @@ +package org.springframework.cloud.bootstrap; + +import java.util.concurrent.atomic.AtomicReference; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; + +/** + * @author Spencer Gibb + */ +@Order(Ordered.HIGHEST_PRECEDENCE) +public class TestHigherPriorityBootstrapConfiguration { + + static final AtomicReference> firstToBeCreated = new AtomicReference<>(); + + public TestHigherPriorityBootstrapConfiguration() { + firstToBeCreated.compareAndSet(null, TestHigherPriorityBootstrapConfiguration.class); + } + +} diff --git a/spring-cloud-context/src/test/resources/META-INF/spring.factories b/spring-cloud-context/src/test/resources/META-INF/spring.factories new file mode 100644 index 00000000..419787be --- /dev/null +++ b/spring-cloud-context/src/test/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +# Bootstrap components +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ +org.springframework.cloud.bootstrap.TestBootstrapConfiguration,\ +org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration