Spring配置文件location的几种设置方法 博客分类: SpringFramework SpringFramework
默认会去加载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>