Update SpringApplicationBuilder to pass args to parent when it's run
Closes gh-5103
This commit is contained in:
parent
c0a2c88da1
commit
29c7b93640
|
@ -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.
|
||||||
|
@ -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;
|
||||||
|
@ -35,6 +36,7 @@ import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.contains;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
@ -103,11 +105,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(),
|
||||||
equalTo(false));
|
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
|
@Test
|
||||||
|
@ -115,11 +121,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(),
|
||||||
equalTo(false));
|
equalTo(false));
|
||||||
|
assertThat(this.context.getParent().getBean(ApplicationArguments.class)
|
||||||
|
.getNonOptionArgs(), contains("a=alpha"));
|
||||||
|
assertThat(this.context.getBean(ApplicationArguments.class).getNonOptionArgs(),
|
||||||
|
contains("b=bravo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue