1. 程式人生 > >Spring 的環境配置以及HelloWorld

Spring 的環境配置以及HelloWorld

API下載地址:http://spring.io/projects/spring-framework


安裝 SPRING TOOL SUITE

SPRING TOOL SUITE 是一個 Eclipse 外掛,利用該外掛可以更方便的在 Eclipse 平臺上開發基於 Spring 的應用。
安裝方法說明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
Help --> Install New Software...
Click Add...
In dialog Add Site dialog, click Archive...
Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip  and click  Open
Clicking OK in the Add Site dialog will bring you back to the dialog 'Install'
Select the xxx/Spring IDE that has appeared
Click Next  and then Finish
Approve the license
Restart eclipse when that is asked

 

然後關閉elipse,再開啟,在preference裡面有Spring就代表著安裝成功

我的是myelipse,我在安裝的過程中,提示,本身就已經安裝了!


jar包


測試類

package cn.com.day01;

public class HelloWorld {
	private String name;

	public void setName(String name) {
		System.out.println("set name....");
		this.name = name;
	}

	public void gethello() {
		System.out.println("你的姓名:" + name);
	}
	public HelloWorld() {
		// TODO Auto-generated constructor stub
		System.out.println("helloworld's construstor....");
	}
}
package cn.com.day01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHello {
	public static void main(String[] args) {
		/*
		 * //1.新建物件
		 *  HelloWorld hh=new HelloWorld(); 
		 * //2.
		 * 賦值 hh.setName("田江南");
		 */
		// 用Spring的話,只需要這樣做就可以了
		// 1.建立spring的IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 2.從容器中獲取bean
		HelloWorld hh = (HelloWorld) ctx.getBean("helloWorld");
		// 3.呼叫方法
		hh.gethello();
	}
}

Spring的配置檔案

src下面新建一個Spring bean configuration的xml檔案 重新命名為applicationContext.xml

對於myelipse的就不一樣了,

右鍵專案-Myelispe-add Spring Capabilities

 

勾選和jar包一致的版本

 

成功之後,專案就變成這樣了

 檔案裡面配置情況如下

<?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">
<!-- class就是Spring要替換的物件的全路徑,id就是類名(第一個字母小寫) -->
<bean id="helloWorld" class="cn.com.day01.HelloWorld">
<!--name是setName的set+名稱,這個名稱的第一個字母小寫  -->
<property name="name" value="Spring"></property>
</bean>

</beans>

如果把測試類的呼叫方法這一行程式碼去掉的話、

package cn.com.day01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHello {
	public static void main(String[] args) {
		/*
		 * //1.新建物件
		 *  HelloWorld hh=new HelloWorld(); 
		 * //2.
		 * 賦值 hh.setName("田江南");
		 */
		// 用Spring的話,只需要這樣做就可以了
		// 1.建立spring的IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 2.從容器中獲取bean
		HelloWorld hh = (HelloWorld) ctx.getBean("helloWorld");
		// 3.呼叫方法
		/*hh.gethello();*/
	}
}

程式首先會建立IOC容器,在配置檔案裡面,先指向類,然後呼叫setName方法賦值,

結果如下


總結:

1.搭配環境

1.1 是elipse就安裝Spring外掛,myelipse一般都是自帶的

1.2 引入jar包4個Spring jar包,一個日誌依賴包

1.3配置檔案

2.一個demo例子,瞭解Spring的作用