转自:http://www.cnblogs.com/shyy/archive/2011/09/29/2453057.html

一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO"); 如果是两个以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"}); 或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");

二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别

classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;

对于FileSystemXmlApplicationContext:
默认表示的是两种:

1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.

如果要使用classpath路径,需要前缀classpath:

public class HelloClient {

protected static final Log log = LogFactory.getLog(HelloClient.class);

public static void main(String[] args) {
// Resource resource = new ClassPathResource("appcontext.xml");
// BeanFactory factory = new XmlBeanFactory(resource); // 用classpath路径
// ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new ClassPathXmlApplicationContext("appcontext.xml"); // ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的
// ApplicationContext factory = new ClassPathXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml"); // 用文件系统的路径,默认指项目的根路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("src/appcontext.xml");
// ApplicationContext factory = new FileSystemXmlApplicationContext("webRoot/WEB-INF/appcontext.xml"); // 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径
// ApplicationContext factory = new FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory = new FileSystemXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml"); // 不加file前缀
ApplicationContext factory = new FileSystemXmlApplicationContext("F:/workspace/example/src/appcontext.xml");
IHelloWorld hw = (IHelloWorld)factory.getBean("helloworldbean");
log.info(hw.getContent("luoshifei"));
}
}

Spring中ClassPathXmlApplicationContext类的简单使用的更多相关文章

  1. java中Color类的简单总结

    java中Color类的简单总结 1.颜色的常识 任何颜色都是由三原色组成(RGB),JAVA中支持224为彩色,即红绿蓝分量取值 介于0-255之间(8位表示) 2.Color类中的常量 publi ...

  2. 转:spring中InitailizingBean接口的简单理解

    转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解   InitializingBean接 ...

  3. spring中WebApplicationContextUtils类说明

    WebApplicationContextUtils是一个抽象类,其提供了一个很便利的方法来获取spring应用的上下文即WebApplicationContext. 其中的静态方法getWebApp ...

  4. 关于Opencv2.4.x中stitcher类的简单应用

    1.opencv2.4以上版本有stitcher类,可以简单方便的实现图像的拼接,目前只是简单的测试一下stitcher类的拼接功能,也是纠结了好长时间,最终发现是要在链接库中加上opencv_sti ...

  5. Spring中一个类的注入和引用是不一样的

    1.在Spring管理下的bean需要以下面这种方式引入(一种注入方式): private MgrService mgrService; public MgrService getMgrService ...

  6. 浅析redis缓存 在spring中的配置 及其简单的使用

    一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...

  7. Qt中QScrollArea类的简单使用心得

           平台:windows 64位        Qt版本:5.5.1 MinGW 32bit 根据自己目前的需求简单说下怎么在QScrollArea滚动窗口中实现多个控件的滚动显示,先看看最 ...

  8. 对QT中QBitArray类进行简单剖析

    我们知道Qt中的QBitArray类支持在位(bit)的层次上进行数据操作.本文剖析该类在二进制文件读写时的一些要点.另外,在Qt中,QDataStream类对于二进制文件的读写提供了诸多便利,需要注 ...

  9. redis—Spring中redis缓存的简单使用

    这里使用的是 Spring-4.3 , redis-2.8 的版本   1.添加maven依赖 <dependency> <groupId>redis.clients</ ...

随机推荐

  1. 如何从ST网站找到对应的固件库

    ST官方网站改版后,基本上很难搜索到固件库的地址,找了半天才找到固件库的下载地址,通过此方法可以找到其他需要的资源,故记下来方便大家. 下载的网站地址为: Home>Tools and Soft ...

  2. CMDB反思2

    当云灭掉CMDB http://blog.vsharing.com/xqscool/A1193910.html 虽然之前也思考过当运维底层都被替换为云时,现有的传统运维可能就消失了,其所依赖的ITIL ...

  3. redis集群的搭建

    1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...

  4. redo文件二

    为什么要引入LGWR后台进程和redo log buffer 如果使用前台进程来将redo的信息写入到redo日志文件组中,那么会导致并发的前台进程对redo日志文件组的争用,从而使用后台进程LGWR ...

  5. C++ 为什么要用覆盖(学习笔记)

    长篇大论这里就不说了,举个例子class fruit{public: void func() { printf("fruit\n"); } virtual void vfunc() ...

  6. gcc编译器基本命令和vi编辑器2

    !1 os fen时 看电影 聊天.支持多核处理器 分任务 已经绝迹cpu Trobe c 分任务操作系统三大组成部分内核,命令解释器(shell外壳),文件系统2修改文件日期或(创建文件)命令:to ...

  7. sql联接那点儿事儿

    1.交叉联接(cross join) select * from t.toy ,b.boy from toy as t cross join boy as b 其效果同select * from t. ...

  8. 线段相交 POJ 2653

    // 线段相交 POJ 2653 // 思路:数据比较水,据说n^2也可以过 // 我是每次枚举线段,和最上面的线段比较 // O(n*m) // #include <bits/stdc++.h ...

  9. Google App Engine Deployment 相关问题

    1.GAE instance上传成功之后可以正常运行的情况下.在Google账户中“关联的应用和网站”,撤销Google App Engine appcfg对Google账户的访问权限. 再次上传时, ...

  10. tomcat log

    $TOMCAT_HOME$/logs/ .out tomcat 启动后的输出日志 ,主要用于输出一些常规的东西,打印的info日志也会在这里输出. 修改tomcat生成的日志位置 在开发/测试环境,日 ...