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
pull/313/merge
Phillip Webb 11 years ago
parent f6f8c26f69
commit 16df38e928

@ -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<ApplicationContextInitializer<?>> 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<ApplicationListener<?>> getListeners() {
return asUnmodifiableSortedSet(this.listeners);
return asUnmodifiableOrderedSet(this.listeners);
}
/**
@ -957,7 +958,7 @@ public class SpringApplication {
}
}
private static <E> Set<E> asUnmodifiableSortedSet(Collection<E> elemements) {
private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elemements) {
List<E> list = new ArrayList<E>();
list.addAll(elemements);
Collections.sort(list, AnnotationAwareOrderComparator.INSTANCE);

@ -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<String> additionalProfiles = new LinkedHashSet<String>();
private Set<ApplicationContextInitializer<?>> initializers = new LinkedHashSet<ApplicationContextInitializer<?>>();
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<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>();
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]));
}
}

Loading…
Cancel
Save