Reputation: 9
i learned that when spring applicationContext is created, context itself will be registered as bean. so i made a simple code and expect applicationContext as a bean. However, when i create applicationContext with java-code like below, i couldn't see applicationContext as a bean.. ====code==== ApplicationContext parent = new GenericXmlApplicationContext(basePath + "parentContext.xml");
GenericApplicationContext child = new GenericApplicationContext(parent);
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(child);
reader.loadBeanDefinitions(basePath+"childContext.xml");
child.refresh();
Printer printer = child.getBean("printer",Printer.class);
assertNotNull(printer);
for(String bean : parent.getBeanDefinitionNames()) {
System.out.println("TTTT : "+ bean +" : "+parent.getBean(bean).getClass().getName());
}
===================== i both tried with parent and child. could anyone can explain why applicationContext itself is not registered as a bean?
Upvotes: 0
Views: 98
Reputation: 330
I would try adding this line to parentContext.xml:
<import resource="contextSub.xml"/>
and in the java code add this annotation
@Before
public void setup(){
ApplicationContext parent = new GenericXmlApplicationContext(basePath + "parentContext.xml");
GenericApplicationContext child = new GenericApplicationContext(parent);
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(child);
reader.loadBeanDefinitions(basePath+"childContext.xml");
child.refresh();
Printer printer = child.getBean("printer",Printer.class);
assertNotNull(printer);
for(String bean : parent.getBeanDefinitionNames()) {
System.out.println("TTTT : "+ bean +" : "+parent.getBean(bean).getClass().getName());
}
Let me know if this helps.
Upvotes: 1