Merge branch '1.3.x'

pull/3499/head
Andy Wilkinson 9 years ago
commit f12bb26649

@ -127,7 +127,7 @@ public class SpringApplicationBuilder {
// If already created we just return the existing context // If already created we just return the existing context
return this.context; return this.context;
} }
configureAsChildIfNecessary(); configureAsChildIfNecessary(args);
if (this.running.compareAndSet(false, true)) { if (this.running.compareAndSet(false, true)) {
synchronized (this.running) { synchronized (this.running) {
// If not already running copy the sources over and then run. // If not already running copy the sources over and then run.
@ -137,14 +137,14 @@ public class SpringApplicationBuilder {
return this.context; return this.context;
} }
private void configureAsChildIfNecessary() { private void configureAsChildIfNecessary(String... args) {
if (this.parent != null && !this.configuredAsChild) { if (this.parent != null && !this.configuredAsChild) {
this.configuredAsChild = true; this.configuredAsChild = true;
if (!this.registerShutdownHookApplied) { if (!this.registerShutdownHookApplied) {
this.application.setRegisterShutdownHook(false); this.application.setRegisterShutdownHook(false);
} }
initializers( initializers(new ParentContextApplicationContextInitializer(
new ParentContextApplicationContextInitializer(this.parent.run())); this.parent.run(args)));
} }
} }
@ -153,7 +153,17 @@ public class SpringApplicationBuilder {
* @return the fully configured {@link SpringApplication}. * @return the fully configured {@link SpringApplication}.
*/ */
public SpringApplication build() { public SpringApplication build() {
configureAsChildIfNecessary(); return build(new String[0]);
}
/**
* Returns a fully configured {@link SpringApplication} that is ready to run. Any
* parent that has been configured will be run with the given {@code args}.
* @param args the parent's args
* @return the fully configured {@link SpringApplication}.
*/
public SpringApplication build(String... args) {
configureAsChildIfNecessary(args);
this.application.setSources(this.sources); this.application.setSources(this.sources);
return this.application; return this.application;
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.Collections;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.test.ApplicationContextTestUtils; import org.springframework.boot.test.ApplicationContextTestUtils;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationContextInitializer;
@ -98,11 +99,15 @@ public class SpringApplicationBuilderTests {
SpringApplicationBuilder application = new SpringApplicationBuilder( SpringApplicationBuilder application = new SpringApplicationBuilder(
ChildConfig.class).contextClass(SpyApplicationContext.class); ChildConfig.class).contextClass(SpyApplicationContext.class);
application.parent(ExampleConfig.class); application.parent(ExampleConfig.class);
this.context = application.run(); this.context = application.run("foo.bar=baz");
verify(((SpyApplicationContext) this.context).getApplicationContext()) verify(((SpyApplicationContext) this.context).getApplicationContext())
.setParent(any(ApplicationContext.class)); .setParent(any(ApplicationContext.class));
assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook()) assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook())
.isFalse(); .isFalse();
assertThat(this.context.getParent().getBean(ApplicationArguments.class)
.getNonOptionArgs()).contains("foo.bar=baz");
assertThat(this.context.getBean(ApplicationArguments.class).getNonOptionArgs())
.contains("foo.bar=baz");
} }
@Test @Test
@ -110,11 +115,15 @@ public class SpringApplicationBuilderTests {
SpringApplicationBuilder application = new SpringApplicationBuilder( SpringApplicationBuilder application = new SpringApplicationBuilder(
ChildConfig.class).contextClass(SpyApplicationContext.class); ChildConfig.class).contextClass(SpyApplicationContext.class);
application.parent(ExampleConfig.class); application.parent(ExampleConfig.class);
this.context = application.build().run(); this.context = application.build("a=alpha").run("b=bravo");
verify(((SpyApplicationContext) this.context).getApplicationContext()) verify(((SpyApplicationContext) this.context).getApplicationContext())
.setParent(any(ApplicationContext.class)); .setParent(any(ApplicationContext.class));
assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook()) assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook())
.isFalse(); .isFalse();
assertThat(this.context.getParent().getBean(ApplicationArguments.class)
.getNonOptionArgs()).contains("a=alpha");
assertThat(this.context.getBean(ApplicationArguments.class).getNonOptionArgs())
.contains("b=bravo");
} }
@Test @Test

Loading…
Cancel
Save