平等关系的配置文件
将配置文件分解为地位平等的多个配置文件,并将所有配置文件的路径定义为一个String数组,将其作为容器
初始化参数出现.
各配置文件间为并列关系,不分主次。
比如以下 spring-school.xml, spring-student.xml就是平等关系配置文件。
spring-student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean id="student" class="com.atChina.Test7.Student" autowire="byType">
<property name="name" value="吴用"/>
<property name="age" value="20" />
</bean>
</beans>
spring-school.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean id="xuexiao" class="com.atChina.Test7.School">
<property name="name" value="同济大学"/>
<property name="address" value="上海市" />
</bean>
</beans>
@Test
public void test1(){
// 加载平等关系的配置文件
String configLocation1 = "com/atChina/Test7/spring-student.xml"; // 类路径的根目录
String configLocation2 = "com/atChina/Test7/spring-school.xml"; // 类路径的根目录
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation1,configLocation2);
Student ss = (Student)ctx.getBean("student");
System.out.println("stduent:"+ss);
}
包含关系的配置文件
total.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!-- 包含关系的配置文件: 一个总的文件,把其他文件包含进来,总的文件一般是不定义bean对象
语法方式: <import resource="其他配置文件的位置">
关键字 "classpath:"表示类路径
<import resource="classpath:com/atChina/Test8/spring-school.xml"/>
<import resource="classpath:com/atChina/Test8/spring-student.xml"/>
-->
<!-- 在包含关系的配置文件中,还可以使用通配符*,表示0或多个字符
使用通配符注意事项:总的文件名称不能包含在通配符的范围内,比如,total.xml不能叫做spring-total.xml,避免造成死循环
-->
<import resource="classpath:com/atChina/Test8/spring-*.xml"/>
</beans>
@Test
public void test1(){
String configLocation = "com/atChina/Test8/total.xml"; // 类路径的根目录
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
Student ss = (Student)ctx.getBean("student");
System.out.println("stduent:"+ss);
}