ITeye博客

Spring的Hello World:理解IoC

istone · 2009-07-11 16:08 · 学习笔记 · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。