Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro23 committed Mar 31, 2020
1 parent ecce3cd commit 6987223
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {}
}

0 comments on commit 6987223

Please sign in to comment.