Make sure bootstrap sources are ordered before use.

Make call to sort() before setting on builder.sources().

fixes gh-176
This commit is contained in:
Spencer Gibb
2017-02-14 14:17:05 -07:00
parent 3269b8df54
commit 6dc35d1edc
5 changed files with 76 additions and 1 deletions

View File

@@ -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);

View File

@@ -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 {
}
}

View File

@@ -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);
}
}

View File

@@ -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<Class<?>> firstToBeCreated = new AtomicReference<>();
public TestHigherPriorityBootstrapConfiguration() {
firstToBeCreated.compareAndSet(null, TestHigherPriorityBootstrapConfiguration.class);
}
}

View File

@@ -0,0 +1,4 @@
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.bootstrap.TestBootstrapConfiguration,\
org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration