Skip to content

ClasspathXmlApplicationContext does not inherit/merge parent context environment [SPR-11068] #15695

@spring-projects-issues

Description

@spring-projects-issues

Martin Gerlach opened SPR-11068 and commented

I have a root ApplicationContext with environment as follows:

StandardServletEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment,/data/myproject/conf/config.properties]}

Then, when I create a new child context as follows:

final ApplicationContext childContext = new ClassPathXmlApplicationContext(new String[] {"classpath:com/example/childcontext.xml"}, parentContext);

its environment shows as follows:

StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[systemProperties,systemEnvironment]}

So, no servlet stuff, no custom properties file in the propertySources.

However, creating the child context as follows behaves different:

final GenericApplicationContext childContext = new GenericApplicationContext(parentContext);
final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(childContext);
xmlReader.loadBeanDefinitions("classpath:com/example/childcontext.xml");
childContext.refresh();

Now, the childContext's environment shows as

StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[systemProperties,systemEnvironment,servletConfigInitParams,servletContextInitParams,jndiProperties,/data/myproject/conf/config.properties]}

From looking at the code, it becomes quite clear why this is: Using the given constructor of ClasspathXmlApplicationContext does not result in a call to AbstractApplicationContext.setParent(ApplicationContext parent) which would merge the environment of the parent into the own environment.


Affects: 3.2.4

Referenced from: commits 863570a, c5fcf19

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions