需求:如果我们在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对象