I have a parent context which i have bootstrapped to my servlet in the web.xml:
<servlet>
<servlet-name>Outflow</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.fmr.bpo.asyncprocessingframework.invocator.wiring.configuration.pojo.common.RootConfig</param-value>
</init-param>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
</servlet>
Since i can only like initialize the child context after loading and setting it's parent context, i'm listening on this parent context by implementing ApplicationListener<ContextRefreshedEvent>
.
Once the parent context loads, my listener's onApplicationEvent
triggers, and i hook up different environments to child contexts and load them:
public void onApplicationEvent(ContextRefreshedEvent event) {
for(String infoKey : clientConfig.getPairs().keySet()) {
PairInfo info = clientConfig.getPairs().get(infoKey);
createChildContext(info);
}
}
private void createChildContext(ListenerInfo info) {
Properties properties = info.getProperties();
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addLast(new PropertiesPropertySource("infoprops", properties));
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(InboundFlow.class);
child.setId(properties.getProperty("id"));
child.setParent(context);
child.setEnvironment(environment);
child.refresh();
}
The problem is that every time this child context refreshes, this same listener is called again (since it implements the ApplicationListener<ContextRefreshedEvent>
). It becomes an infinite loop coz every time a context loads, the onApplicationEvent
is creates another child context which calls the event method again.
How do i avoid this by listening in on just the parent context and not all contexts? Or is there any other way for me to initialize my child contexts?
Thanks in advance for the help.