From 598819bd66f5526263f8232ff91253957fd9fdde Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 21 Apr 2015 08:19:36 +0200 Subject: [PATCH] Add support for @Order in BootstrapConfiguration --- docs/src/main/asciidoc/spring-cloud-commons.adoc | 11 ++++++++++- .../cloud/bootstrap/BootstrapApplicationListener.java | 6 ++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index c5fbb357..7fc76152 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -129,7 +129,16 @@ a comma-separated list of Spring `@Configuration` classes which will be used to create the context. Any beans that you want to be available to the main application context for autowiring can be created here, and also there is a special contract for `@Beans` of type -`ApplicationContextInitializer`. +`ApplicationContextInitializer`. Classes can be marked with an `@Order` +if you want to control the startup sequence (the default order is +"last"). + +WARNING: Be careful when adding custom `BootstrapConfiguration` that the +classes you add are not `@ComponentScanned` by mistake into your +"main" application context, where they might not be needed. +Use a separate package name for boot configuration classes that is +not already covered by your `@ComponentScan` or `@SpringBootApplication` +annotated configuration classes. The bootstrap process ends by injecting initializers into the main `SpringApplication` instance (i.e. the normal Spring Boot startup 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 39e0d553..3b4aa2d7 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 @@ -48,7 +48,7 @@ import org.springframework.util.StringUtils; * spring.factories as {@link BootstrapConfiguration}, and initialized with external * config taken from "bootstrap.properties" (or yml), instead of the normal * "application.properties". - * + * * @author Dave Syer * */ @@ -122,6 +122,7 @@ public class BootstrapApplicationListener implements sources.add(cls); } builder.sources(sources.toArray(new Class[sources.size()])); + AnnotationAwareOrderComparator.sort(sources); final ConfigurableApplicationContext context = builder.run(); // Make the bootstrap context a parent of the app context addAncestorInitializer(application, context); @@ -203,7 +204,8 @@ public class BootstrapApplicationListener implements while (context.getParent() != null && context.getParent() != context) { context = (ConfigurableApplicationContext) context.getParent(); } - new ParentContextApplicationContextInitializer(parent).initialize(context); + new ParentContextApplicationContextInitializer(this.parent) + .initialize(context); } private void preemptMerge(MutablePropertySources propertySources,