---
title: "Spring多配置文件的用法【入门知识】"
author: "jackcode"
date: "2009-03-15T23:14:50Z"
tags: []
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2009-03-15T23:16:25Z"
id: 349539
url: "https://jackcode.iteye.com/blog/349539"
---

方法一，使用主从配置文件的结构，可以用import标签，
比如：<import resource="serviceContext.xml"/>
方法二，如果你使用的是tomcat服务器的话，在web.xml里面，添加多个spring配置文件即可。比如，
<context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>
         /WEB-INF/classes/context1.xml，
         /WEB-INF/classes/context2.xml，
         /WEB-INF/classes/context3.xml
        </param-value>
</context-param>

注，建议使用第一种用法。因为，如果要在代码里面手动加载配置文件，只需要加载主文件就行，不用每个配置文件都要加上。就是说，不必出现以下这种情况：
ApplicationContext context = new ClassPathXmlApplicationContext(
				new String{
                                "spring.xml",
                                "spring1.xml",
                                "spring2.xml",
                                "spring3.xml"
				});
只需要，
ApplicationContext context = new ClassPathXmlApplicationContext(
				"spring.xml");