我们常用的加载context文件的方法有如下三个:

1、FileSystemXmlApplicationContext

这个方法是从文件绝对路径加载配置文件,例如:

ApplicationContext ctx = new FileSystemXmlApplicationContext( "G:/Test/applicationcontext.xml ");

如果在参数中写的不是绝对路径,那么方法调用的时候也会默认用绝对路径来找,我测试的时候发现默认的绝对路径是eclipse所在的路径。

采用绝对路径的话,程序的灵活性就很差了,所以这个方法一般不推荐。

(如果要使用classpath路径,需要加入前缀classpath:   )

2、ClassPathXmlApplicationContext

这个方法是从classpath下加载配置文件(适合于相对路径方式加载),例如:

ApplicationContext ctx = new ClassPathXmlApplicationContext( "/applicationcontext.xml ");

该方法参数中classpath: 前缀是不需要的,默认就是指项目的classpath路径下面;这也就是说用ClassPathXmlApplicationContext时默认的根目录是在WEB-INF/classes下面,而不是项目根目录。这个需要注意!

3、XmlWebApplicationContext

专为web工程定制的方法,推荐Web项目中使用。例如:

ServletContext servletContext = request.getSession().getServletContext();

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

Spring 加载配置文件的方式的更多相关文章

  1. spring加载配置文件

    spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...

  2. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  3. Spring笔记02(3种加载配置文件的方式)

    1.不使用Spring的实例: 01.Animal接口对应的代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃饭 St ...

  4. SpringMvc的创建流程以及2种加载配置文件的方式

    1.首先创建个web项目,第一步导入相应的jar包,并且buildtoPath 2.用elipse或myeclipse点击进入web.xml中 按住 Alt+ / 有个提示 找到前面带 #Dispat ...

  5. Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...

  6. Java 加载配置文件的方式

    一 使用原生方式读取配置文件 1 文件系统加载 Java代码   InputStream in = new FileInputStream("config.properties") ...

  7. spring 加载配置文件的相关配置总结

    PropertyPlaceholderConfigurer      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:p ...

  8. SpringBoot加载配置文件(@PropertySource@importSource@Value)

    情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...

  9. Spring加载Properties配置文件的三种方式

    一.通过 context:property-placeholder 标签实现配置文件加载 1) 用法: 1.在spring.xml配置文件中添加标签 <context:property-plac ...

随机推荐

  1. [mysql] MySQL Order By Rand()效率【转载】

    最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...

  2. Spark 快速理解

    转自:http://blog.csdn.net/colorant/article/details/8255958 ==是什么 == 目标Scope(解决什么问题) 在大规模的特定数据集上的迭代运算或重 ...

  3. 如何退出Activity?如何安全退出已调用多个Activity的Application?

    对于单一Activity的应用来说,退出很简单,直接finish()即可. 1.抛异常强制退出: 该方法通过抛异常,使程序ForceClose. 验证可以,但是,需要解决的问题是,如何使程序结束掉,而 ...

  4. Spring零散所得

    Spring容器中bean的id或name,都可以有多个,且第一个为标识符(Qualifier),其余皆为别名(Alias).所以都可以通过applicationContext.getBean(&qu ...

  5. linux -- ubuntu 14.10开机出现错误“Error found when loading /root/.profile”解决

    修改完root权限自动登录后,发现开机出现以下提示: Error found when loading /root/.profile stdin:is not a tty ………… 解决方法:在终端中 ...

  6. Python的可变对象与不可变对象。

    参考:http://thomaschen2011.iteye.com/blog/1441254          Python基础:Python可变对象和不可变对象 http://blog.jobbo ...

  7. Ubuntu 14.04 安装R 环境

    Introduction R is a popular open source programming language that specializes in statistical computi ...

  8. 支付宝(移动支付)服务端java版

    所需支付宝jar包: sdk2-2.0.jar(点击下载) 工具类目录结构:   点击下载 商户信息已经公钥私钥的配置(公钥私钥的生成与支付宝商户平台配置请看官方文档:https://doc.open ...

  9. 学习 TList 类的实现[4]

    现在准备一步步地模拟 TList 类, 建立一个自己的 TMyList. 首先, 这个类中应该包括前面提到的那个 Pointer 数组(TPointerList)的指针(PPointerList): ...

  10. PHPCMS列表循环序列号自增标签代码

    {pc:content action="position"posid="1"num="3"thumb="1"} {php ...