8.xml文件导入其他xml文件配置(多个xml文件建立联系)

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

Student类///
public class Student {
	private long id;
	private String name;
	private int age;
	
	public Student(long id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
		System.out.println("有参构造器");
	}
	public Student() {
		System.out.println("in Student()");
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}
Teacher类///
public class Teacher {
	private long id;
	private String name;
	private Student student;
	public Teacher() {}
	public Teacher(Student student) {
		this.student = student;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
}

student.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    <bean name="stu" class="com.briup.bean.Student">
    	<property name="id" value="1"></property>
    	<property name="name" value="kkk"></property>
    	<property name="age" value="20"></property>
    </bean>
</beans>

teacher.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
     <bean name="teacher" class="com.briup.bean.Teacher">
    	<property name="id" value="1"></property>
    	<property name="name" value="jack"></property>
    	<property name="student" ref="stu"></property>
    </bean>
</beans>

方式一:各自加载各自的xml文件(从当前运行的内存中获取)

两个分开的xml文件,但是teacher.xml中要进行注入student。
知识点:
通过spring初始化的数据,不管在哪些xml中初始化的, 当需要依赖的时候,都是从当前运行的内存中获取对应的数据

测试类

//知识点:一个xml配置文件中 导入另外一个xml配置文件
@Test
public void ioc_imp() {
	try {
		//同时加载两个xml文件
		//加载xml文件路径的数组,不分先后顺序,是在内存中获取对象的
		String[] path = {"com/briup/ioc/imp/teacher.xml","com/briup/ioc/imp/student.xml"};
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
		Teacher t = (Teacher) container.getBean("teacher");
		System.out.println(t);
		System.out.println(t.getStudent());
	} catch (Exception e) {
		e.printStackTrace();
	}
}

结果:能够拿到Student和Teacher对象
在这里插入图片描述

方式二:由一个文件加载多个xml文件,用import标签

import.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
     	<!-- resource="路径",写出要加载的xml文件的相对路径 -->
        <import resource="student.xml"/>
        <import resource="teacher.xml"/>
</beans>

测试类:

@Test
public void ioc_imp() {
	try {
		//只需要加载一个文件
		String[] path = {"com/briup/ioc/imp/import.xml"};
		ApplicationContext container = new ClassPathXmlApplicationContext(path);
		Teacher t = (Teacher) container.getBean("teacher");
		System.out.println(t);
		System.out.println(t.getStudent());
	} catch (Exception e) {
		e.printStackTrace();
	}
}

结果:能够拿到Student和Teacher对象
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值