spring的配置文件在web.xml中加载的方式
2012-10-26 15:00
711 查看
web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式。
1.如果spring配置文件的名称为applicationContext.xml,并且存放在WEB-INF/目录下,那么只需要在web.xml中加入以下代码即可
该监听器会自动扫描WEB-INF/ 下的applicationContext.xrnl 文件,这种方式多数用在只有一个配置文件的情况下。
还可以使用Spring的ContextLoaderServlet的这个特殊Servlet,实现代码如下
上面的两种方式都可以,不过二者有一些区别。
2.如果spring配置文件的名称是自定义的其他名称,比如为applicationContext-test.xml,也还是存放在WEB-INF/目录下,那么还需要配置contextConfigLocation这个参数,该参数是一个字符串,监听器或者Servlet会自定将该字符串按照特定的字符(比如空格、逗号、分号)解析成多个文件。需要添加如下代码:
如果上面嫌得麻烦,可以使用通配符对上面进行简写如下
然后再加入以上的监听器或者servlet代码就可以。
3.如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例.
1.如果spring配置文件的名称为applicationContext.xml,并且存放在WEB-INF/目录下,那么只需要在web.xml中加入以下代码即可
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
该监听器会自动扫描WEB-INF/ 下的applicationContext.xrnl 文件,这种方式多数用在只有一个配置文件的情况下。
还可以使用Spring的ContextLoaderServlet的这个特殊Servlet,实现代码如下
<servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name> <load-on-startup>1(比较小的数字就可以)</load-on-startup> </servlet>
上面的两种方式都可以,不过二者有一些区别。
2.如果spring配置文件的名称是自定义的其他名称,比如为applicationContext-test.xml,也还是存放在WEB-INF/目录下,那么还需要配置contextConfigLocation这个参数,该参数是一个字符串,监听器或者Servlet会自定将该字符串按照特定的字符(比如空格、逗号、分号)解析成多个文件。需要添加如下代码:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-testA.xml,/WEB-INF/application-testB.xml,/WEB-INF/application-testB.xml</param-value> </context-param>
如果上面嫌得麻烦,可以使用通配符对上面进行简写如下
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application*.xml</param-value> </context-param>
然后再加入以上的监听器或者servlet代码就可以。
3.如果既没有applicationContext.xml 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法加载配置文件或无法正常创建ApplicationContext 实例.
相关文章推荐
- spring的配置文件在web.xml中加载的方式
- spring的配置文件在web.xml中加载的方式
- spring的配置文件在web.xml中加载的方式
- Spring中加载xml配置文件的方式
- Spring加载xml配置文件的方式总结
- spring 中加载xml配置文件的方式
- spring与servlet3.0无web.xml文件时的配置方式
- spring中加载xml配置文件的方式
- Spring配置文件 在web.xml的几种配置方式
- Spring加载xml配置文件的方式
- web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序
- web.xml文件中配置( listenr, filter,servlet, spring)的加载顺序 研究(转)
- Spring 中加载XML配置文件的方式
- Spring中加载XML配置文件的6种方式
- Spring中加载XML配置文件的方式
- spring中加载xml配置文件的方式
- web.xml 文件中一般包括 servlet, spring, filter, listenr的配置。那么他们是按照一个什么顺序加载呢?
- Spring中加载XML配置文件的方式
- 通过web.xml文件自动启动spring容器加载对应的配置文件
- spring 中加载xml配置文件的方式