一、简单的用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"));
  }
}

1. FileSystemXmlApplicationContext

pplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); //加载单个配置文件

String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new FileSystemXmlApplicationContext(locations ); //加载多个配置文件

ApplicationContext ctx =new FileSystemXmlApplicationContext("D:roject/bean.xml");//根据具体路径加载文件

2. ClassPathXmlApplicationContext

pplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");

String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new ClassPathXmlApplication(locations);
注:其中FileSystemXmlApplicationContext和ClassPathXmlApplicationContext与BeanFactory的xml文件定位方式一样是基于路径的。

3. XmlWebApplicationContext
ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

spring mvc使用ClassPathXmlApplicationContext或FileSystemXmlApplicationContext和XmlWebApplicationContext类的操作其中 XmlWebApplicationContext是专为Web工程定制的。的更多相关文章

  1. spring mvc DispatcherServlet详解前传---HttpServletBean类

    从上章里我们已经看到: DispatcherServlet extends FrameworkServlet FrameworkServlet extends HttpServletBean impl ...

  2. spring MVC 如何查找URL对应的处理类

    在spring 3.1之前,查找URL相应的处理方法,需要分两步,第一步是调用DefaultAnnotationHandlerMapping,查找到相应的controller类,第二步,再调用Anno ...

  3. Spring MVC 学习总结(十一)——IDEA+Maven+多模块实现SSM框架集成

    一.SSM概要 与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC ...

  4. java spring mvc restful 上传文件

    spring mvc 配置文件 <bean class="com.baiyyy.yfz.core.RestfulHandlerMethodMapping" />     ...

  5. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  6. Spring MVC 学习总结(六)——Spring+Spring MVC+MyBatis框架集成

    与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...

  7. Spring MVC 学习总结(四)——视图与综合示例

    一.表单标签库 1.1.简介 从Spring2.0起就提供了一组全面的自动数据绑定标签来处理表单元素.生成的标签兼容HTML 4.01与XHTML 1.0.表单标签库中包含了可以用在JSP页面中渲染H ...

  8. Spring MVC异常处理详解

    Spring MVC中异常处理的类体系结构 下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要 ...

  9. [转] 使用Spring MVC构建REST风格WEB应用

    原文地址:http://fancy888.iteye.com/blog/1629120 对于运行在网络上的MIS系统而言,处理数据的是整个系统的主要任务,翻开程序我们可以看到,80%以上的代码都在处理 ...

随机推荐

  1. Visual Studio 2013 在使用 MVC5 无智能提示

    关于 Visual Studio 2013 在使用 MVC5 无智能提示的问题,类库无法正常识别,连最基本的关键字提示都没有了,类变色也没有了,所有的关键字代码,类名,方法成员名都要全部手动敲 原因: ...

  2. 【开源】前端练手笔记,Chrome扩展应用程序(html+CSS+JS) (1)

    项目名称:github-notification 项目地址:https://github.com/WQTeam/github-notification 说明:本人打算抽时间学习前端(html + cs ...

  3. wndows常用命令

    1. 远程桌面 mstsc (Microsoft terminal services client)

  4. [r]Seven habits of effective text editing

    Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...

  5. BZOJ 3223 文艺平衡树

    Description   您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2  ...

  6. Ajax页面逻辑

    逻辑上模拟整个与服务器通信的过程.在没有真正与服务器通信的时候,如何写这样的ajax请求. 先根据页面结构创建一个静态数据(JSON) var arrival_address_data={" ...

  7. Response对象

    Response对象来自HttpResponse类,它用于向客户端输出信息或设置客户端输出状态,使用Response对象可以直接发送信息给浏览器.重定向浏览器到另一个URL或设置cookie的值等. ...

  8. 海量日志数据提取某日访问百度次数最多的那个IP的Java实现

    海量日志数据提取某日访问百度次数最多的那个IP的Java实现 前几天在网上看到july的一篇文章<教你如何迅速秒杀掉:99%的海量数据处理面试题>,里面说到百度的一个面试题目,题目如下: ...

  9. VS2010安装Visual Assist

    Visual Assist X是一款非常好的Microsoft Visual Studio 2005和Visual Studio .NET插件,支持C/C++,C#,ASP,Visual Basic, ...

  10. bzoj3668

    noi水题,直接推出来每一位上取什么值才能取1直接搞就可以了 ..] of longint; x,n,i,j,k,t,m,ans:longint; fl:boolean; s:string; ch:c ...