---
title: "Spring的Hello World：理解IoC"
author: "istone"
date: "2009-07-11T16:08:30Z"
tags: []
slug: "spring-hello-world-ioc"
draft: false
lastmod: "2009-07-13T14:02:37Z"
id: 423866
url: "https://istone.iteye.com/blog/423866"
---

```
package action;

import junit.framework.TestCase;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.HelloService;

public class HelloAction extends TestCase {

	public void testContext() {
		ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml"); 
		HelloService hello = (HelloService) context.getBean("helloService");
		
		hello.print();
		System.out.println("context");
	}
	
	public void testFactory() throws Exception {
		ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext("hello.xml"); 
		BeanFactory factory = resource; 

		HelloService hello = (HelloService) factory.getBean("helloService");
		hello.print();
		System.out.println("factory");
	}
}
```

 

注：将spring.jar、junit.jar、commons-logging.jar复制进WEB-INF/lib