Spring配置文件详解 – applicationContext.xml文件路径

Java编程                

spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码

1
2
3
4
5
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

spring就会被自动加载

但在实际的开发过程中,我们可能需要调整applicationContext.xml的位置,以使程序结构更加的清晰。在web.xml中,配置Spring配置文件的代码如下:

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>这里写路劲</param-value>
</context-param>

根据Spring框架的API描述,有以下四种方法配置applicationContext.xml文件路径

1. /WEB-INF/applicationContext.xml

2. com/config/applicationContext.xml

3. file:C:/javacode/springdemo/com/config/applicationContext.xml

4. classpath:com/config/applicationContext.xml

注:以上路径只是举例,具体使用还是要针对真是项目的,做编程的这点举一反三能力还是有的吧

开发过程中,如果spring的配置文件applicationContext.xml未加载的话,一般回报这样的错误

Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

下面就有我为大家举例自定义applicationContext.xml路径的常见的方法。

1、Spring配置文件在WEB-INF下面

这种情况你可以不去管他,不进行配置,因为spring会默认去加载,如果一定要配置呢,可以这样

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

2、Spring配置文件在WEB-INF下的某个文件夹下,比如config下,可以这样配置

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/config/applicationContext.xml</param-value>
</context-param>

3、Spring配置文件在src下面,可以这样配置

1
2
3
4
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>

或者

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

4、Spring配置文件在src下的某个包里,比如com.config,可以这样配置

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/com/config/applicationContext.xml</param-value>
</context-param>

或者

1
2
3
4
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/config/applicationContext.xml</param-value>
</context-param>
 
 
 
 
 

Spring配置文件详解 – applicationContext.xml文件路径的更多相关文章

  1. Spring配置文件详解 - applicationContext.xml文件路径

    spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...

  2. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  3. J2EE进阶(四)Spring配置文件详解

    J2EE进阶(四)Spring配置文件详解 前言 Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程 ...

  4. Spring中加载ApplicationContext.xml文件的方式

    Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...

  5. [转载]Spring配置文件详解一:

    原文地址:与base-package="com.xx">Spring配置文件详解一:<context:annotation-config/>与<contex ...

  6. spring配置文件详解以及beans:beans标签

    第一行的意思就是你这个文件的默认schema为security,所以你的beans定义就需要加上前缀beans 一般的定义文件默认都是beans: 下面是spring配置文件的详解: 转自:http: ...

  7. Spring配置文件详解

      转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...

  8. Spring 配置文件详解 (以2.5为例)

    转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847          Spring配置文件是用于指导Spring工厂进行Bean生 ...

  9. Spring学习总结(3)——Spring配置文件详解

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

随机推荐

  1. dinic模板

    procedure addedge(u,v,cap:longint); begin sid[tot].u:=u; sid[tot].v:=v; sid[tot].cap:=cap; sid[tot]. ...

  2. Chrome 自动填充的表单是淡黄色的背景

    Chrome 自动填充的表单是淡黄色的背景解决方案; input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px #fff inset; - ...

  3. Servlet作业2-将表单提交的商品信息输出到页面中

    1,表单页面 shangpin.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  4. ITEM M6 自增(INCREMENT)、自减(DECREMENT)操作符前缀形式与后缀形式的区别

    前缀自增 UPInt & UPint::operator++() { *this+=1; return *this; } 后缀自增 const UPInt & UPint::opera ...

  5. html和xhtml和html5一些区别和笔记

    XHTML,HTML XHTML 与 HTML 4.01 标准没有太多的不同, XHTML1.0是XML风格的HTML4.01,标签的规范更加严格 , W3C一直是推崇XHTML这种严格类型的语法, ...

  6. mindmup-editabletable-编辑table的使用

    官方网站:http://mindmup.github.io/editable-table/ github下载地址:https://github.com/mindmup/editable-table 我 ...

  7. Android与H5交互(java与js的交互)

    一.理论概述 1.js调用java方法 直接调用WebView的该方法就可以添加接口了,不过先要启动交互 // 启用javascript mWebView.getSettings().setJavaS ...

  8. HTTP协议学习---(五)压缩

    HTTP压缩是指: Web服务器和浏览器之间压缩传输的"文本内容"的方法. HTTP采用通用的压缩算法,比如gzip来压缩HTML,Javascript, CSS文件. 能大大减少 ...

  9. Matlab读入含有特殊分隔符的文件(textread)

    笔者在此基础上进行运行,修改得到以下内容,希望大家给与补充: textread 基本语法是: [A,B,C,…] = textread(filename,format) [A,B,C,…] = tex ...

  10. Android成长日记-ListView

    数据适配器:把复杂的数据(数组,链表,数据库,集合等)填充在指定的视图界面上 适配器的类型: ① ArrayAdapter(数组适配器):用于绑定格式单一的数据 数据源:可以是集合或数组 ① Simp ...