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

spring 中location设置方法, 通过在网络搜索, 发现有下面几个方法。 另外具体的参数值, 也可以有决定路径, 相对路径等区分, 见后面

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.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>

 

关于location路径的设置常见形式

1、相对路径
可以通过classpath和classpath*设置、如:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
<property name="mapperLocations" value="classpath*:mapper/**/*Mapper.xml"></property>
</bean>

<context:property-placeholder location="classpath:/jdbc.properties" />

关于classpath和classpath*的区别请参照:
1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
3.文件名字包含通配符资源(如果spring-*.xml,spring*.xml),   如果根目录为"", classpath加载不到任何资源, 而classpath*则可以加载到classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源

详细参照:http://blog.csdn.net/zl3450341/article/details/9306983

2、绝对路径(可以把配置文件放到工程目录以外、如tomcat和jboss的bin目录下:这样做的目的是隔离开发环境和发布环境的配置文件、将差异化配置放到war以外、方便发布)
可以通过file设置、如:
<property name="locations"  value="file:D/tomcat6/bin/db.properties" />

也可以通过file加系统变量的方式设置、如
在tomcat6下bin中的catalina.sh/catalina.bat 中 (或者jboss的run.sh/run.bat )文件中、JAVA——OPTS的后面追加

-Djdbc.properties=D:/tomcat6/bin/db.properties

<property name="locations"  value="file:#{systemProperties['jdbc.properties']}" />

发表评论