Browse Source

Investigating

pull/70/merge
Dave Syer 13 years ago
parent
commit
a082f2bed5
  1. 23
      spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java
  2. 6
      spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java
  3. 17
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationBuilderTests.java
  4. 1
      spring-boot/src/test/resources/application-node.properties
  5. 1
      spring-boot/src/test/resources/logback-test.xml

23
spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java

@ -99,7 +99,7 @@ public class SpringApplicationBuilder { @@ -99,7 +99,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(new ParentContextApplicationContextInitializer(
addInitializers(true, new ParentContextApplicationContextInitializer(
this.parent.run(args)));
}
@ -111,6 +111,8 @@ public class SpringApplicationBuilder { @@ -111,6 +111,8 @@ public class SpringApplicationBuilder {
if (this.running.compareAndSet(false, true)) {
synchronized (this.running) {
// If not already running copy the sources over and then run.
// this.application.setDefaultArgs(this.defaultArgs
// .toArray(new String[this.defaultArgs.size()]));
this.application.setSources(this.sources);
this.context = this.application.run(args);
}
@ -190,7 +192,7 @@ public class SpringApplicationBuilder { @@ -190,7 +192,7 @@ public class SpringApplicationBuilder {
this.parent = new SpringApplicationBuilder();
this.parent.context = parent;
this.parent.running.set(true);
addInitializers(new ParentContextApplicationContextInitializer(parent));
addInitializers(true, new ParentContextApplicationContextInitializer(parent));
return this;
}
@ -394,17 +396,24 @@ public class SpringApplicationBuilder { @@ -394,17 +396,24 @@ public class SpringApplicationBuilder {
*/
public SpringApplicationBuilder initializers(
ApplicationContextInitializer<?>... initializers) {
addInitializers(initializers);
addInitializers(false, initializers);
return this;
}
/**
* @param initializers the initializers to add
*/
private void addInitializers(ApplicationContextInitializer<?>... initializers) {
Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>(
this.application.getInitializers());
target.addAll(Arrays.asList(initializers));
private void addInitializers(boolean prepend,
ApplicationContextInitializer<?>... initializers) {
Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>();
if (prepend) {
target.addAll(Arrays.asList(initializers));
target.addAll(this.application.getInitializers());
}
else {
target.addAll(this.application.getInitializers());
target.addAll(Arrays.asList(initializers));
}
this.application.setInitializers(target);
}

6
spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java

@ -171,7 +171,7 @@ public class ConfigFileApplicationContextInitializer implements @@ -171,7 +171,7 @@ public class ConfigFileApplicationContextInitializer implements
}
Resource resource = resourceLoader.getResource(location);
PropertySource<?> propertySource = getPropertySource(resource, loaders);
PropertySource<?> propertySource = getPropertySource(resource, profile, loaders);
if (propertySource == null) {
return;
}
@ -187,9 +187,9 @@ public class ConfigFileApplicationContextInitializer implements @@ -187,9 +187,9 @@ public class ConfigFileApplicationContextInitializer implements
environment.getPropertySources().addLast(propertySource);
}
private PropertySource<?> getPropertySource(Resource resource,
private PropertySource<?> getPropertySource(Resource resource, String profile,
List<PropertySourceLoader> loaders) {
String key = resource.getDescription();
String key = resource.getDescription() + (profile == null ? "" : "#" + profile);
if (this.cached.containsKey(key)) {
return this.cached.get(key);
}

17
spring-boot/src/test/java/org/springframework/boot/SpringApplicationBuilderTests.java

@ -87,6 +87,23 @@ public class SpringApplicationBuilderTests { @@ -87,6 +87,23 @@ public class SpringApplicationBuilderTests {
any(ApplicationContext.class));
}
@Test
public void parentFirstCreationWithProfileAndDefaultArgs() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder(
ExampleConfig.class).profiles("node").defaultArgs("--transport=redis")
.child(ChildConfig.class).web(false);
this.context = application.run();
assertThat(this.context.getEnvironment().acceptsProfiles("node"), is(true));
assertThat(this.context.getEnvironment().getProperty("transport"),
is(equalTo("redis")));
assertThat(this.context.getParent().getEnvironment().acceptsProfiles("node"),
is(true));
assertThat(this.context.getParent().getEnvironment().getProperty("transport"),
is(equalTo("redis")));
// only defined in node profile
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("spam")));
}
@Test
public void parentContextIdentical() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder(

1
spring-boot/src/test/resources/application-node.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
bar: spam

1
spring-boot/src/test/resources/logback-test.xml

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
</encoder>
</appender>
<logger name="org.springframework.boot.context.annotation" level="TRACE" />
<logger name="org.springframework.boot.context.initializer" level="TRACE" />
<logger name="org.springframework.boot.config" level="TRACE" />
<logger name="org.thymeleaf" level="TRACE" />
<root level="INFO">

Loading…
Cancel
Save