您的位置:首页 > 移动开发

Spring applicationContext.xml 文件 路径配置 (2)

2017-05-02 14:46 661 查看

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

spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码
org.springframework.web.context.ContextLoaderListener
spring就会被自动加载但在实际的开发过程中,我们可能需要调整applicationContext.xml的位置,以使程序结构更加的清晰。在web.xml中,配置Spring配置文件的代码如下:
contextConfigLocation这里写路劲
根据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会默认去加载,如果一定要配置呢,可以这样
contextConfigLocation
WEB-INF/applicationContext.xml
2、Spring配置文件在WEB-INF下的某个文件夹下,比如config下,可以这样配置
contextConfigLocation
WEB-INF/config/applicationContext.xml
3、Spring配置文件在src下面,可以这样配置
conte96c2xtConfigLocation
WEB-INF/classes/applicationContext.xml
或者
contextConfigLocation
classpath:applicationContext.xml
4、Spring配置文件在src下的某个包里,比如com.config,可以这样配置
contextConfigLocation
WEB-INF/classes/com/config/applicationContext.xml
或者
contextConfigLocation
classpath:com/config/applicationContext.xml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: