首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Spring 多配置文件加載三種方式:數組、通配符、import 標籤

     編輯:Java 程式設計入門

場景:模塊化配置管理

隨項目增長,單一 applicationContext.xml 膨脹難維護。拆分為功能維度配置文件:

config/
├── datasource.xml      # 數據源、連接池、事務管理器
├── mybatis.xml         # SqlSessionFactory、Mapper 掃描
├── redis.xml           # RedisTemplate、序列化、集群配置
├── mq.xml              # RabbitMQ/Kafka ConnectionFactory、監聽容器
├── security.xml        # Spring Security 鏈、權限表達式
└── applicationContext.xml  # 總裝配入口

方式一:構造器傳入字符串數組 (程序化)

// 適用於單元測試、獨立應用、動態決定配置集合
String[] configLocations = {
    "classpath:config/datasource.xml",
    "classpath:config/mybatis.xml",
    "classpath:config/redis.xml",
    "classpath:config/mq.xml",
    "classpath:config/security.xml"
};
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
  • 優點:類型安全、IDE 重構友好、可運行時動態組裝 (如根據 Profile 選擇)。
  • 缺點:部署時需重新編譯/打包修改數組內容。

方式二:Classpath 通配符 (聲明式、最常用)

<!-- applicationContext.xml -->
<import resource="classpath*:config/*.xml"/>

<!-- 或構造器 -->
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:config/*.xml");
  • 匹配規則:classpath*: 掃描**所有類加載器** (含 JAR 包) 下匹配路徑;classpath: 僅當前類加載器。
  • 加載順序不確定:依賴文件系統/ JAR 內部順序。**有依賴關系的配置文件勿用通配符**,改用显式 <import> 或數組指定順序。
  • 性能:啟動時掃描所有 JAR,大型項目可觀。生產環境建議顯式列出。

方式三:<import> 標籤組合 (聲明式、順序可控、最推薦)

<!-- applicationContext.xml 總入口 -->
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:config/datasource.xml"/>
    <import resource="classpath:config/mybatis.xml"/>
    <import resource="classpath:config/redis.xml"/>
    <import resource="classpath:config/mq.xml"/>
    <import resource="classpath:config/security.xml"/>

    <!-- 覆蓋/補充 Bean 定義 -->
    <bean id="customBean" class="..."/>
</beans>
  • 順序語義:按 <import> 出現順序加載,**後加載的 Bean 定義覆蓋先前同名 Bean** (利用此特性實現環境差異化覆蓋)。
  • 相對路徑解析:resource 路徑相對**當前配置文件位置**。config/datasource.xml 解析為 classpath:config/datasource.xml。
  • 跨 JAR 導入:classpath:com/example/shared/datasource.xml 可從依賴 JAR 中導入共享配置。

Spring Boot 時代的配置加載 (對比參考)

維度Spring XML 時代Spring Boot (JavaConfig + YAML)
入口applicationContext.xml + <import>@SpringBootApplication 掃描包
分模塊多 XML 文件@Configuration 類 + @Import / @EnableAutoConfiguration
環境隔離applicationContext-dev.xml 覆蓋application-dev.yml / @Profile("dev")
外部化PropertyPlaceholderConfigurer@ConfigurationProperties + Environment 抽象
條件裝配BeanPostProcessor 手工@ConditionalOnClass/Property/Bean/MissingBean

最佳實踐清單

  1. 核心基礎設施配置 (數據源、事務、Redis、MQ) 顯式 <import>,保證加載順序與依賴關系清晰。
  2. 業務模塊配置:若模塊獨立部署為 JAR,在 JAR 中提供 spring.factories 或 META-INF/spring/components.xml,主工程通過 classpath*:/META-INF/spring/*.xml 自動發現 (Spring Boot 自動裝配原理)。
  3. 環境差異化:基礎配置不變,環境專用配置 (如 datasource-prod.xml) 通過 <import resource="${env}/datasource.xml"/> + JVM 參數 -Denv=config-prod 動態切換。
  4. 避免循環導入:A import B, B import A → 容器啟動拋 BeanDefinitionParsingException。分層架構單向依賴。
  5. 大型項目啟動優化:減少通配符掃描、合並小配置文件、啟用 spring.context.indexer (編譯期生成候選索引)。

總結

Spring 多配置文件加載三板斧:

  • 數組:程序化控制,測試/動態場景。
  • 通配符:約定優於配置,**注意順序不確定與 JAR 掃描性能**。
  • import 標籤:**生產環境首選**,順序可控、依賴顯式、支持相對路徑與跨 JAR 復用。

現代 Spring Boot 已以 **JavaConfig + YAML + Profile + Condition** 替代 XML,但**模塊化拆分、順序控制、覆蓋語義、跨模塊復用**的底層邏輯完全一致。理解 XML 時代的三種方式,能更透徹地掌握 Boot 自動裝配的本質。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved