1、簡單的用ApplicationContext作測試的話,得到Spring中定義的Bean實例(對象).能夠用:web
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");app
若是是兩個以上: ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});測試
或者用通配符: ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");spa
2、ClassPathXmlApplicationContext[只能讀放在web-info/classes目錄下的配置文件]和FileSystemXmlApplicationContext的區別code
classpath:前綴是不須要的,默認就是指項目的classpath路徑下面; 若是要使用絕對路徑,須要加上file:前綴表示這是絕對路徑;xml
對於FileSystemXmlApplicationContext: 默認表示的是兩種:對象
1.沒有盤符的是項目工做路徑,即項目的根目錄; 2.有盤符表示的是文件絕對路徑.get
若是要使用classpath路徑,須要前綴classpath:io
public class HelloClient {class
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"));
} }