Skip to content

Commit 6987223

Browse files
committed
1 parent ecce3cd commit 6987223

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spring-context/src/main/java/io/micronaut/spring/context/MicronautApplicationContext.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.context.*;
3131
import org.springframework.core.ResolvableType;
3232
import org.springframework.core.env.ConfigurableEnvironment;
33+
import org.springframework.core.env.Environment;
3334
import org.springframework.core.io.ProtocolResolver;
3435
import org.springframework.core.io.Resource;
3536
import org.springframework.core.io.UrlResource;
@@ -320,6 +321,12 @@ public void setId(String id) {
320321
@Override
321322
public void setParent(ApplicationContext parent) {
322323
this.parent = parent;
324+
if (parent != null) {
325+
Environment parentEnvironment = parent.getEnvironment();
326+
if (parentEnvironment instanceof ConfigurableEnvironment) {
327+
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
328+
}
329+
}
323330
}
324331

325332
@Override

spring-context/src/test/groovy/io/micronaut/spring/annotation/context/ApplicationContextSpec.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.micronaut.spring.context.MicronautApplicationContext
2020
import org.springframework.beans.factory.BeanFactoryUtils
2121
import org.springframework.context.ApplicationContext
2222
import org.springframework.context.support.GenericApplicationContext
23+
import org.springframework.core.env.MapPropertySource
2324
import org.springframework.stereotype.Service
2425
import spock.lang.Specification
2526

@@ -95,5 +96,26 @@ class ApplicationContextSpec extends Specification {
9596

9697
}
9798

99+
void "test set parent context"() {
100+
ApplicationContext parent = new GenericApplicationContext()
101+
parent.environment.propertySources.addFirst(new MapPropertySource("parentPropertySource", ["parent.property": "parentValue"]))
102+
103+
when:
104+
MicronautApplicationContext context = new MicronautApplicationContext(
105+
io.micronaut.context.ApplicationContext.build()
106+
.properties("child.property": 'childValue (${parent.property})')
107+
)
108+
// only a started MicronautApplicationContext has its environment set
109+
// (if not using the dependency injection constructor) - does this make sense?
110+
context.start()
111+
context.setParent(parent)
112+
context.environment.getRequiredProperty("parent.property")
113+
String childProperty = context.environment.getRequiredProperty("child.property")
114+
String childPropertyResolved = context.environment.resolveRequiredPlaceholders(childProperty)
115+
116+
then:
117+
childPropertyResolved == "childValue (parentValue)"
118+
}
119+
98120
static class MySingleton {}
99121
}

0 commit comments

Comments
 (0)