1、準備須要的jar包:
核心jar包:下載的spring-framework-3.X.X.RELEASE-with-docs.zip中dist
依賴的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