spring中装载多个配置文件

本文详细解析了如何在web.xml中配置Spring框架,包括两种主要方式:直接在servlet中配置和使用ContextLoaderListener配置。此外,文章还介绍了其他配置Spring的方式,如ClassPathApplicationContext和通过父配置文件导入子配置文件。最后,提供了关于配置Spring的实践建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,web.xml中

<!--方式一 在servlet中-->
<servlet>
<servlet-name>spring-config</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring-config.xml,/WEB-INF/applicationContext.xml</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>


<!--方式二 配置监听-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2.使用ClassPathApplicationContext。这个类会自动从classpath目录中加载所有的配置文件,不过这个类好像只适用本地调试时用的。

3.通过一个父配置文件将所有子配置文件导入。
在配置文件中有一个标签import,它能把其它的bean定义配置文件导入到父文件夹中

4.通过FileSystemXmlApplicationContext
FileSystemXmlApplicationContext的构造函数是一个字符串数组这个数组就是保存配置文件的路径
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值