diff --git a/spring-context/src/main/java/io/micronaut/spring/context/MicronautApplicationContext.java b/spring-context/src/main/java/io/micronaut/spring/context/MicronautApplicationContext.java index 75e5a853a..98dda885a 100644 --- a/spring-context/src/main/java/io/micronaut/spring/context/MicronautApplicationContext.java +++ b/spring-context/src/main/java/io/micronaut/spring/context/MicronautApplicationContext.java @@ -30,6 +30,7 @@ import org.springframework.context.*; import org.springframework.core.ResolvableType; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; import org.springframework.core.io.ProtocolResolver; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; @@ -320,6 +321,12 @@ public void setId(String id) { @Override public void setParent(ApplicationContext parent) { this.parent = parent; + if (parent != null) { + Environment parentEnvironment = parent.getEnvironment(); + if (parentEnvironment instanceof ConfigurableEnvironment) { + getEnvironment().merge((ConfigurableEnvironment) parentEnvironment); + } + } } @Override diff --git a/spring-context/src/test/groovy/io/micronaut/spring/annotation/context/ApplicationContextSpec.groovy b/spring-context/src/test/groovy/io/micronaut/spring/annotation/context/ApplicationContextSpec.groovy index dd9a6f816..3e9e08375 100644 --- a/spring-context/src/test/groovy/io/micronaut/spring/annotation/context/ApplicationContextSpec.groovy +++ b/spring-context/src/test/groovy/io/micronaut/spring/annotation/context/ApplicationContextSpec.groovy @@ -20,6 +20,7 @@ import io.micronaut.spring.context.MicronautApplicationContext import org.springframework.beans.factory.BeanFactoryUtils import org.springframework.context.ApplicationContext import org.springframework.context.support.GenericApplicationContext +import org.springframework.core.env.MapPropertySource import org.springframework.stereotype.Service import spock.lang.Specification @@ -95,5 +96,26 @@ class ApplicationContextSpec extends Specification { } + void "test set parent context"() { + ApplicationContext parent = new GenericApplicationContext() + parent.environment.propertySources.addFirst(new MapPropertySource("parentPropertySource", ["parent.property": "parentValue"])) + + when: + MicronautApplicationContext context = new MicronautApplicationContext( + io.micronaut.context.ApplicationContext.build() + .properties("child.property": 'childValue (${parent.property})') + ) + // only a started MicronautApplicationContext has its environment set + // (if not using the dependency injection constructor) - does this make sense? + context.start() + context.setParent(parent) + context.environment.getRequiredProperty("parent.property") + String childProperty = context.environment.getRequiredProperty("child.property") + String childPropertyResolved = context.environment.resolveRequiredPlaceholders(childProperty) + + then: + childPropertyResolved == "childValue (parentValue)" + } + static class MySingleton {} }