Spring Hello World之旅

1、准备须要的jar包:

核心jar包:下载的spring-framework-3.X.X.RELEASE-with-docs.zipdist
依赖的jar包
下载的spring-framework-3.X.X.RELEASE-dependencies.zip java

2、建立标准Java工程:

1.建立标准的java工程(JRE环境选择1.6);
2.将下载的jar包导入到工程中;
3.在src目录下建立一个xml文件,可命名为applicationContext.xml,内容以下:
spring

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


</beans>

4.建立HelloWorld.java文件
app

public class HelloWorld {
	public void sayHello(){
		System.out.println("hello world!!!");
	}
}

5.在xml中添加测试

<bean id="hello" class="chenhao.HelloWorld"></bean>

6.给工程添加junit3,而后写测试类Test
spa

import junit.framework.TestCase;

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

import chenhao.HelloWorld;

public class Test extends TestCase {

	private BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

	public void hello(){
		HelloWorld hello = (HelloWorld) factory.getBean("hello");
		hello.sayHello();
	}
}



测试hello方法:
结果: hello world!!!
code

相关文章
相关标签/搜索