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 {
if (this.parent != null) { if (this.parent != null) {
// If there is a parent initialize it and make sure it is added to the current // If there is a parent initialize it and make sure it is added to the current
// context // context
addInitializers(new ParentContextApplicationContextInitializer( addInitializers(true, new ParentContextApplicationContextInitializer(
this.parent.run(args))); this.parent.run(args)));
} }
@ -111,6 +111,8 @@ public class SpringApplicationBuilder {
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.
// this.application.setDefaultArgs(this.defaultArgs
// .toArray(new String[this.defaultArgs.size()]));
this.application.setSources(this.sources); this.application.setSources(this.sources);
this.context = this.application.run(args); this.context = this.application.run(args);
} }
@ -190,7 +192,7 @@ public class SpringApplicationBuilder {
this.parent = new SpringApplicationBuilder(); this.parent = new SpringApplicationBuilder();
this.parent.context = parent; this.parent.context = parent;
this.parent.running.set(true); this.parent.running.set(true);
addInitializers(new ParentContextApplicationContextInitializer(parent)); addInitializers(true, new ParentContextApplicationContextInitializer(parent));
return this; return this;
} }
@ -394,17 +396,24 @@ public class SpringApplicationBuilder {
*/ */
public SpringApplicationBuilder initializers( public SpringApplicationBuilder initializers(
ApplicationContextInitializer<?>... initializers) { ApplicationContextInitializer<?>... initializers) {
addInitializers(initializers); addInitializers(false, initializers);
return this; return this;
} }
/** /**
* @param initializers the initializers to add * @param initializers the initializers to add
*/ */
private void addInitializers(ApplicationContextInitializer<?>... initializers) { private void addInitializers(boolean prepend,
Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>( ApplicationContextInitializer<?>... initializers) {
this.application.getInitializers()); Set<ApplicationContextInitializer<?>> target = new LinkedHashSet<ApplicationContextInitializer<?>>();
target.addAll(Arrays.asList(initializers)); 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); 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
} }
Resource resource = resourceLoader.getResource(location); Resource resource = resourceLoader.getResource(location);
PropertySource<?> propertySource = getPropertySource(resource, loaders); PropertySource<?> propertySource = getPropertySource(resource, profile, loaders);
if (propertySource == null) { if (propertySource == null) {
return; return;
} }
@ -187,9 +187,9 @@ public class ConfigFileApplicationContextInitializer implements
environment.getPropertySources().addLast(propertySource); environment.getPropertySources().addLast(propertySource);
} }
private PropertySource<?> getPropertySource(Resource resource, private PropertySource<?> getPropertySource(Resource resource, String profile,
List<PropertySourceLoader> loaders) { List<PropertySourceLoader> loaders) {
String key = resource.getDescription(); String key = resource.getDescription() + (profile == null ? "" : "#" + profile);
if (this.cached.containsKey(key)) { if (this.cached.containsKey(key)) {
return this.cached.get(key); return this.cached.get(key);
} }

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

@ -87,6 +87,23 @@ public class SpringApplicationBuilderTests {
any(ApplicationContext.class)); 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 @Test
public void parentContextIdentical() throws Exception { public void parentContextIdentical() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder( SpringApplicationBuilder application = new SpringApplicationBuilder(

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

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

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

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

Loading…
Cancel
Save