SpringIOC注入模块中xml文件导入其他xml文件配置


如果我们在spring框架中配置了多个xml文件,我们可以在读取配置文件的时候把这些xml文件一下全都读取
也可以只读一个总的xml文件,在这个总的xml文件中把其他的xml全都都导入进来。

例如:
student.xml文件:

<bean name="student" class="com.briup.bean.Student">
<property name="id">
<value>25</value>
</property>
</bean>

teacher.xml文件:

<bean name="teacher" class="com.briup.bean.Teacher">
<property name="student" ref="student"></property>
</bean>

import.xml文件:

<import resource="teacher.xml"/>
<import resource="student.xml"/>

main:

String[] path = {"com/briup/ioc/imp/import.xml"};
ApplicationContext container = 
new ClassPathXmlApplicationContext(path);

Teacher t = (Teacher) container.getBean("teacher");
System.out.println(t.getStudent());

猜你喜欢

转载自www.cnblogs.com/Magic-Li/p/11723226.html