From 16df38e928258f51d8f1a75015d30edbf4dd096f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 5 Feb 2014 15:36:06 -0800 Subject: [PATCH] Ordering of Application Initializer and Listeners Update javadoc and method names to indicate that ApplicationInitializer and ApplicationListeners are `Order` sorted before being invoked. Also remove specific sorting from SpringApplicationBuilder, relying instead on the Order sorting. Fixes gh-316 --- .../boot/SpringApplication.java | 15 ++++---- .../builder/SpringApplicationBuilder.java | 35 ++----------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index d650d9fa00..7aa8fedd57 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -826,12 +826,12 @@ public class SpringApplication { } /** - * Returns read-only set of the {@link ApplicationContextInitializer}s that will be - * applied to the Spring {@link ApplicationContext}. + * Returns read-only ordered Set of the {@link ApplicationContextInitializer}s that + * will be applied to the Spring {@link ApplicationContext}. * @return the initializers */ public Set> getInitializers() { - return asUnmodifiableSortedSet(this.initializers); + return asUnmodifiableOrderedSet(this.initializers); } /** @@ -854,12 +854,13 @@ public class SpringApplication { } /** - * Returns readonly set of the {@link ApplicationListener}s that will be applied to - * the SpringApplication and registered with the {@link ApplicationContext}. + * Returns read-only ordered Set of the {@link ApplicationListener}s that will be + * applied to the SpringApplication and registered with the {@link ApplicationContext} + * . * @return the listeners */ public Set> getListeners() { - return asUnmodifiableSortedSet(this.listeners); + return asUnmodifiableOrderedSet(this.listeners); } /** @@ -957,7 +958,7 @@ public class SpringApplication { } } - private static Set asUnmodifiableSortedSet(Collection elemements) { + private static Set asUnmodifiableOrderedSet(Collection elemements) { List list = new ArrayList(); list.addAll(elemements); Collections.sort(list, AnnotationAwareOrderComparator.INSTANCE); diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index e160e94d1c..d841dc410d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -31,7 +31,6 @@ import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.boot.ProfileDetector; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.ParentContextApplicationContextInitializer; -import org.springframework.boot.context.web.ServletContextApplicationContextInitializer; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationListener; @@ -80,8 +79,6 @@ public class SpringApplicationBuilder { private Set additionalProfiles = new LinkedHashSet(); - private Set> initializers = new LinkedHashSet>(); - public SpringApplicationBuilder(Object... sources) { this.application = new SpringApplication(sources); } @@ -114,7 +111,7 @@ public class SpringApplicationBuilder { if (this.parent != null) { // If there is a parent initialize it and make sure it is added to the current // context - addInitializers(true, new ParentContextApplicationContextInitializer( + initializers(new ParentContextApplicationContextInitializer( this.parent.run(args))); } @@ -203,7 +200,7 @@ public class SpringApplicationBuilder { this.parent = new SpringApplicationBuilder(); this.parent.context = parent; this.parent.running.set(true); - addInitializers(true, new ParentContextApplicationContextInitializer(parent)); + initializers(new ParentContextApplicationContextInitializer(parent)); return this; } @@ -454,14 +451,7 @@ public class SpringApplicationBuilder { */ public SpringApplicationBuilder initializers( ApplicationContextInitializer... initializers) { - for (ApplicationContextInitializer initializer : initializers) { - boolean prepend = false; - if (initializer instanceof ParentContextApplicationContextInitializer - || initializer instanceof ServletContextApplicationContextInitializer) { - prepend = true; - } - addInitializers(prepend, initializer); - } + this.application.addInitializers(initializers); return this; } @@ -478,23 +468,4 @@ public class SpringApplicationBuilder { return this; } - /** - * @param initializers the initializers to add - */ - private void addInitializers(boolean prepend, - ApplicationContextInitializer... initializers) { - Set> target = new LinkedHashSet>(); - if (prepend) { - target.addAll(Arrays.asList(initializers)); - target.addAll(this.initializers); - } - else { - target.addAll(this.initializers); - target.addAll(Arrays.asList(initializers)); - } - this.initializers = target; - this.application.addInitializers(target - .toArray(new ApplicationContextInitializer[0])); - } - }