spring11:为应用指定多个spring配置文件

本文探讨了Spring框架中配置文件的不同组织方式,包括平等关系与包含关系的配置文件。平等关系下,多个配置文件地位相同,各自定义不同的Bean;而包含关系则通过一个总配置文件导入其他配置文件。

  平等关系的配置文件

  将配置文件分解为地位平等的多个配置文件,并将所有配置文件的路径定义为一个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);
	
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值