2024年5月29日发(作者:)

如何使用多个Spring的xml配置文件(多模块配置)

在用Struts Spring Hibernate进行项目团队开发的时候,我们每个开发人员可能都

会用到spring的配置文件,如果我们每个开放人员都用同一个的话,这样会给项目开发带

来一定的影响,不过大家不用急,Spring为我们提供了很好的方法,我们可以写多个xml

文件,然后将每个文件引入进来就行了,话不多说了,马上开始!!

方法一、在中定义 contextConfigLocation参数.spring会使用这个参数加

载.所有”逗号”分割的xml.如果没有这个参数,spring默认加载

WEB-INF/文件(我们现在将Spring配置文件都放在WEB-INF

下面)

contextConfigLocation

classpath*:conf/spring/applicationContext_core*.xml,

classpath*:conf/spring/applicationContext_dict*.xml,

classpath*:conf/spring/applicationContext_,

classpath*:conf/spring/applicationContext_staff*.xml,

classpath*:conf/spring/applicationContext_

classpath*:conf/spring/applicationContext_modules*.xml

classpath*:conf/spring/applicationContext_cti*.xml

classpath*:conf/spring/applicationContext_apm*.xml

contextConfigLocation 参数定义了要装入的 Spring 配置文件。

详细说明如下:利用ServletContextListener 实现。

Spring 提供tContextListener 的一个实现类

tLoaderListener ,该类可以作为listener

使用,它会在创建时自动查找WEB-INF/ 下的 文件。

因此,如果只有一个配置文件,并且文件名为 ,则只需在

文件中增加如下代码即可:

tLoaderListener