Reputation: 3
I want to know how to connect(reference) parent-xml and child-xml in spring.
I have two xml which one is context.xml(parent-xml) and the other is contextSub.xml(child-xml). Also, I have one test class.
What I want is that without writing setup() method, how to connect(or reference) two xml(parent-xml and child-xml)?
In setup() method, there are some logic(cording) to make context.xml child-xml connect(to connect context.xml and child-xml). But I think there is another way to connect them. Alredy, I tried that I wrote
import resource="classpath:/test/src/main/resources/context.xml"
in contextSub.xml, but it didn't work. Could you please let me know? Is there any wrong cording?
*******context.xml *******
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.nana.service.Hello">
<property name="name" >
<value>HongSangJik</value>
</property>
<property name="printer" ref="consolePrint" />
</bean>
<bean id="consolePrint" class="com.nana.service.ConsolePrinter" />
****** contextSub.xml ******
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloSub" class="com.nana.service.Hello">
<property name="name">
<value>HongGilDong</value>
</property>
<property name="printer" ref="consolePrint" />
</bean>
******** HelloTest.java ********
package com.nana.service;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/context.xml")
public class HelloTest {
ApplicationContext context;
ApplicationContext subContext;
Hello hello;
Hello hellosub;
@Before
public void setup() {
context=
new GenericXmlApplicationContext("/context.xml");
hello = context.getBean("hello", Hello.class);
GenericApplicationContext child=
new GenericApplicationContext(context);
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(child);
reader.loadBeanDefinitions("contextSub.xml");
child.refresh();
hellosub=child.getBean("helloSub", Hello.class);
}
@Test
public void sayHelloTest() {
assertThat("Hello!HongSangJik", is(hello.sayHello()));
assertThat("Hello!HongGilDong", is(hellosub.sayHello()));
}
}
---------------After updating setup() code [HelloTest]
package com.nana.service;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/context.xml")
public class HelloTest {
ApplicationContext context;
ApplicationContext subContext;
Hello hello;
Hello hellosub;
@Before
public void setup() {
context= new GenericXmlApplicationContext("/context.xml");
hello = context.getBean("hello", Hello.class);
hellosub=context.getBean("helloSub", Hello.class);
}
@Test
public void sayHelloTest() {
assertThat("Hello!HongSangJik", is(hello.sayHello()));
assertThat("Hello!HongGilDong", is(hellosub.sayHello()));
}
}
****[context.xml]****
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloSub" class="com.nana.service.Hello">
<property name="name">
<value>HongGilDong</value>
</property>
<property name="printer" ref="consolePrint" />
</bean>
Upvotes: 0
Views: 989
Reputation: 136
Since your context.xml is parent-xml and contextSub.xml is child-xml, I would try adding this line to context.xml:
<import resource="contextSub.xml"/>
and remove the <import....>
from contextSub.xml file.
In the java code we only need to load "context.xml" file.
@Before
public void setup()
{
context= new GenericXmlApplicationContext("/context.xml");
hello = context.getBean("hello", Hello.class);
hellosub=context.getBean("helloSub", Hello.class);
}
Upvotes: 0