양영준
양영준

Reputation: 9

about spring applicationContext

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

Answers (1)

Ajay Negi
Ajay Negi

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

Related Questions