日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/6/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。