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 e0e0728e50a..7578e89e722 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 @@ -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"); * you may not use this file except in compliance with the License. @@ -127,7 +127,7 @@ public class SpringApplicationBuilder { // If already created we just return the existing context return this.context; } - configureAsChildIfNecessary(); + configureAsChildIfNecessary(args); if (this.running.compareAndSet(false, true)) { synchronized (this.running) { // If not already running copy the sources over and then run. @@ -137,14 +137,14 @@ public class SpringApplicationBuilder { return this.context; } - private void configureAsChildIfNecessary() { + private void configureAsChildIfNecessary(String... args) { if (this.parent != null && !this.configuredAsChild) { this.configuredAsChild = true; if (!this.registerShutdownHookApplied) { this.application.setRegisterShutdownHook(false); } - initializers( - new ParentContextApplicationContextInitializer(this.parent.run())); + initializers(new ParentContextApplicationContextInitializer( + this.parent.run(args))); } } @@ -153,7 +153,17 @@ public class SpringApplicationBuilder { * @return the fully configured {@link SpringApplication}. */ 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); return this.application; } diff --git a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index f6dbb00d8ab..7967755522c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -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"); * 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.Test; +import org.springframework.boot.ApplicationArguments; import org.springframework.boot.test.ApplicationContextTestUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -35,6 +36,7 @@ import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.util.StringUtils; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -103,11 +105,15 @@ public class SpringApplicationBuilderTests { SpringApplicationBuilder application = new SpringApplicationBuilder( ChildConfig.class).contextClass(SpyApplicationContext.class); application.parent(ExampleConfig.class); - this.context = application.run(); + this.context = application.run("foo.bar=baz"); verify(((SpyApplicationContext) this.context).getApplicationContext()) .setParent(any(ApplicationContext.class)); assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook(), equalTo(false)); + assertThat(this.context.getParent().getBean(ApplicationArguments.class) + .getNonOptionArgs(), contains("foo.bar=baz")); + assertThat(this.context.getBean(ApplicationArguments.class).getNonOptionArgs(), + contains("foo.bar=baz")); } @Test @@ -115,11 +121,15 @@ public class SpringApplicationBuilderTests { SpringApplicationBuilder application = new SpringApplicationBuilder( ChildConfig.class).contextClass(SpyApplicationContext.class); application.parent(ExampleConfig.class); - this.context = application.build().run(); + this.context = application.build("a=alpha").run("b=bravo"); verify(((SpyApplicationContext) this.context).getApplicationContext()) .setParent(any(ApplicationContext.class)); assertThat(((SpyApplicationContext) this.context).getRegisteredShutdownHook(), equalTo(false)); + assertThat(this.context.getParent().getBean(ApplicationArguments.class) + .getNonOptionArgs(), contains("a=alpha")); + assertThat(this.context.getBean(ApplicationArguments.class).getNonOptionArgs(), + contains("b=bravo")); } @Test