spring11:为应用指定多个spring配置文件
生活随笔
收集整理的這篇文章主要介紹了
spring11:为应用指定多个spring配置文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 平等關系的配置文件
? 將配置文件分解為地位平等的多個配置文件,并將所有配置文件的路徑定義為一個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> @Testpublic 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> @Testpublic 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);}?
?
總結
以上是生活随笔為你收集整理的spring11:为应用指定多个spring配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring10: 引用类型的自动注入
- 下一篇: spring12:注解的方式实现di(依