diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java index aff694fd..44a76aa0 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/named/NamedContextFactory.java @@ -16,18 +16,20 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.core.env.MapPropertySource; /** - * Creates a set of child contexts that allows a set of Specifications to define the - * beans in each child context. + * Creates a set of child contexts that allows a set of Specifications to define the beans + * in each child context. * * Ported from spring-cloud-netflix FeignClientFactory and SpringClientFactory * * @author Spencer Gibb * @author Dave Syer */ -public abstract class NamedContextFactory implements DisposableBean, ApplicationContextAware { +public abstract class NamedContextFactory + implements DisposableBean, ApplicationContextAware { public interface Specification { String getName(); + Class[] getConfiguration(); } @@ -41,8 +43,8 @@ public abstract class NamedContextFactory defaultConfigType, String propertySourceName, String propertyName) { + public NamedContextFactory(Class defaultConfigType, String propertySourceName, + String propertyName) { this.defaultConfigType = defaultConfigType; this.propertySourceName = propertySourceName; this.propertyName = propertyName; @@ -63,6 +65,8 @@ public abstract class NamedContextFactory values = this.contexts.values(); for (AnnotationConfigApplicationContext context : values) { + // This can fail, but it never throws an exception (you see stack traces + // logged as WARN). context.close(); } this.contexts.clear(); @@ -87,8 +91,7 @@ public abstract class NamedContextFactory entry : this.configurations - .entrySet()) { + for (Map.Entry entry : this.configurations.entrySet()) { if (entry.getKey().startsWith("default.")) { for (Class configuration : entry.getValue().getConfiguration()) { context.register(configuration); @@ -97,11 +100,9 @@ public abstract class NamedContextFactory singletonMap(propertyName, name))); + context.getEnvironment().getPropertySources().addFirst(new MapPropertySource( + this.propertySourceName, + Collections. singletonMap(this.propertyName, name))); if (this.parent != null) { // Uses Environment from parent as well as beans context.setParent(this.parent);