Spring配置文件location的几种设置方法

本文介绍了Spring框架中配置文件的多种加载方式,包括默认加载、通过servlet-name自定义、通过DispatcherServlet的init-param自定义、通过ContextLoaderListener自定义等。此外还详细解释了如何确保/WEB-INF/applicationContext.xml被正确加载。

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

1.location

会去加WEB-INF下的applicationContext.xml文件,如果文件不存在,会抛出以下的异常。

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

 

2. web.xml中通过servlet name自定义

以下的定,会去加WEB-INF下面的test-servlet.xmlspring的配置文件

 

<servlet>
	<servlet-name>test</servlet-name>		
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>test</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

 

3.web.xml通过DispatcherServletinit-param自定

通过以下的定义,会去加载src/config文件夹下的test-servlet.xml作为spring的配置文件

 

<servlet>  
    <servlet-name>test</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/test-servlet.xml</param-value>  
    </init-param>  
</servlet>  
<servlet-mapping>  
    <servlet-name>test</servlet-name>  
    <url-pattern>*.do</url-pattern>  
</servlet-mapping>  

 

4.web.xml通过ContextLoaderListener自定

以下的定,会去加WEB-INF文件夹下的test-servlet.xmlspring的配置文件

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/test-servlet.xml</param-value>
</context-param>

注:其他的配置文件也可以用这种方法设定,比如要去加载 src/config/文件夹下的test-context.xml

 

<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:config/test-context.xml</param-value>  
</context-param>

 

注意点

如果就是想将/WEB-INF/applicationContext.xml作为配置文件呢。

单单使用<listener><context-param>是不行的。因为你要让Servlet去处理某种请求(URL,比如*.do),就必须得定义<servlet>,这种情况下,完整的web.xml如下

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<display-name>SpringFormTagTest</display-name>
	
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
	
	<servlet>
		<servlet-name>test</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value> 
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>test</servlet-name>
		<url-pattern>*.*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>jsp/input.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值