---
title: "Spring中获取ApplicationContextt.xml配置文件的接口"
author: "crabdave"
date: "2017-05-08T12:11:09Z"
tags: []
categories: ["开源软件"]
slug: "spring-applicationcontextt-xml"
draft: false
lastmod: "2017-05-08T12:16:23Z"
id: 2373140
url: "https://crabdave.iteye.com/blog/2373140"
---

**Spring中获取ApplicationContextt.xml配置文件的接口**

 

   参考：[http://www.blogjava.net/xcp/archive/2011/06/22/352821.html](http://www.blogjava.net/xcp/archive/2011/06/22/352821.html)

    

    // 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"));