默认会去加载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.xml作为spring的配置文件
<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中通过DispatcherServlet的init-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.xml作为spring的配置文件
<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>
相关推荐
spring配置文件实例
Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合
spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件...
使用Spring配置文件实现AOP
spring 配置文件 归类 IOC AOPspring 配置文件 归类 IOC AOP
spring 配置 文件
spring配置文件详解,Spring的applicationContext.xml文件代码:
spring配置文件加密实现
Spring配置 Spring 配置 映射 加注释!!!!Spring配置 Spring 配置 映射 加注释!!!!
spring配置文件详解,交你如何理解spring,熟练运用spring
Spring配置文件:注入各种数据时配置文件的配置格式
spring配置文件详细介绍 通过对xml配置的熟练掌握 相信spring真的太简单
在这篇文章里对于SpringXML配置我将向你展示12种比较好实战其中些实战不仅是好实战更是必要实战除 此以外还有其他原因例如领域模型设计都能影响XML配置但是这篇文章重点研究XML配置易读性和易管理性
大型项目的struts,spring配置文件
Spring框架的一个配置文件。Spring框架主要解决了创建对象和管理对象的问题!
本word详细介绍了spring各种配置文件,相信对于初学spring的各位大侠是个很好的阶梯、
官方spring.xml中约束的引用说明文档,配置spring文件的时候添加约束所需要的文档说明.
本文档是很专业的 Spring动态加载配置文件,实现热部署方案。