spring多配置文件用法总结

发表于: java/j2ee | 作者: darkmi | 日期: 2011/06/01 08:00
字号:

在定义spring配置文件的时候将不同模块的配置放在不同的配置文件中,这样可以使程序的结构更加清晰。总结了一下spring中使用多配置文件的方法。

方法一:
在web.xml文件中将所有配置文件引入:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		classpath*:/applicationContext.xml
		classpath*:/applicationContext-memcached.xml
	</param-value>
</context-param>

<!--Spring的ApplicationContext 载入 -->
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

说明:

(1)如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml文件中增加如下代码即可:

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

(2)contextConfigLocation用来指定多个配置文件,这些配置文件可以使用逗号、分号、空格作为分隔符,或者一个配置文件一行也可以,Spring 的ContextLoaderListener 负责将该字符串分解成多个配置文件。

方法二:
使用import标签:

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

切换主题切换主题
返回顶部