web.xml加载spring配置文件的两种方式

第一种

1, 在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applicationContext.xml文件

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath*:conf/spring/applicationContext_core*.xml,
    classpath*:conf/spring/applicationContext_dict*.xml,
    classpath*:conf/spring/applicationContext_hibernate.xml,
    classpath*:conf/spring/applicationContext_staff*.xml,
    classpath*:conf/spring/applicationContext_security.xml
    classpath*:conf/spring/applicationContext_modules*.xml
    classpath*:conf/spring/applicationContext_cti*.xml
    classpath*:conf/spring/applicationContext_apm*.xml
  </param-value>
</context-param> 

contextConfigLocation 参数定义了要装入的 Spring 配置文件。原说明如下:、利用ServletContextListener 实现。Spring 提供ServletContextListener 的一个实现类ContextLoaderListener ,该类可以作
为listener 使用,它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。因
此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml
文件中增加如下代码即可:

org.springframework.web.context.ContextLoaderListener 如果有多个配置文件需要载入,则考虑使用

<!-- XML 文件的文件头–〉

<?xml version="l.O" encoding="工80-8859-1"?>
<!-- web.xm1 文件的DTD 等信息--〉
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN"
''http://java.sun.com/dtd/web-app_2_3.dtd''>
<web-app>
<!--确定多个配置文件-->
	<context-param>
		<!-- 参数名为contextConfigLocation…--〉
		<param-name>contextConfigLocation</param-name>
		<!一多个配置文件之间以,隔开--〉
		<param-value>
			/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>
	<!-- 采用listener创建Applicat工onContext 实例-->
	<listener>
  		<listener-class>org.sprngframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

第二种:在DispatcherServlet里的加载

<servlet>
   <servlet-name>mvc-dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <!-- 配置springMVC需要加载的配置文件
      spring-dao.xml,spring-service.xml,spring-web.xml
      Mybatis - > spring -> springmvc-->
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/spring/spring-dao.xml,
         classpath*:/spring/spring-service.xml,
         classpath*:/spring/spring-web.xml</param-value>
   </init-param>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值