1- 介紹
本教程文章是基於以下編寫的:
- Spring Framework 4.0.4 RELEASE
- Eclipse 4.5 MARS (ok for Eclipse 4.4 LUNA)
在本文中使用Maven來聲明Spring庫,而不是下載Spring庫,並以正常的方式來聲明。
Maven是一個工具,可以幫你自動,高效地管理您的庫,它已成為慣例,所有 Java 程式員必須知道。如果你不知道如何使用Maven,可以花10分鐘就學會如何使用它:
如果你想下載Spring和聲明庫,您可以用傳統的方式見附錄在檔的結尾。
2- Spring框架
下圖顯示了Spring框架的結構。


- IoC Container: 這是最重要的,也是最基礎的, Spring的基礎。它的作用是配置和Java對象的生命週期管理。這篇教學中我們將學習這一部分。
- DAO, ORM, AOP, WEB: 該模組可用於將工具或框架集成到了Spring。
2.1- 反轉控制和依賴注入
要瞭解這個問題,我們使用以下幾類:
// Interface HelloWorld public interface HelloWorld { public void sayHello(); } // Class implements HelloWorld public class SpringHelloWorld implements HelloWorld { public void sayHello() { System.out.println("Spring say Hello!"); } } // Other class implements HelloWorld public class StrutsHelloWorld implements HelloWorld { public void sayHello() { System.out.println("Struts say Hello!"); } } // And Service class public class HelloWorldService { // Field type HelloWorld private HelloWorld helloWorld; // Constructor HelloWorldService // It initializes the values for the field 'helloWorld' public HelloWorldService() { this.helloWorld = new StrutsHelloWorld(); } }
顯而易見的是 HelloWorldService 類管理創建 HelloWorld 對象。
- 另外,在上述情況下,當 HelloWorldService 對象從它的構造創建時,HelloWorld對象也被創建了。 它是從StrutsHelloWorld 創建。
現在的問題是,您要創建一個HelloWorldService對象,HelloWorld對象也同時被創建,但它必須是SpringHelloWorld。
所以 HelloWorldService 是控制“對象創建” Hello World 的。我們為什麼不創建 Hello World 轉讓由第三方,
而是使用 HelloWorldService ?因為我們有“反轉控制”(IOC)的定義。
並且IoC容器將充當管理者角色,創建了HelloWorldService 和 HelloWorld 。
IoC = Inversion of Control

IoC容器創建 HelloWorldService 對象,是通過 setter 方法傳遞 HelloWorld 對象到HelloWorldService。IoC容器做的是“依賴注入”到HelloWorldService。這裏的相關性是指對象之間的依賴關係: HelloWorldService 和 helloWorld.
在這一點上,我們已經明確了什麼是 IoC和DI。讓我們舉個例子來更好的理解。
3- 創建專案
- File/New/Other...


輸入:
- Group Id: com.zaixian
- Artifact Id: HelloSpring

您的專案已創建:

確保您的專案是建立在Java7或更高版本。右鍵單擊該專案並選擇屬性。




4- 聲明Spring的基礎庫
這是 Spring的 HelloWorld 例子,所以我們只使用基本的Spring庫(核心)。打開pom.xml檔來將使用的庫聲明:
- pom.xml 使用以下內容重新覆蓋原上面的內容。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zaixian</groupId> <artifactId>HelloSpring</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
5- 工程代碼

- HelloWorld.java
package com.zaixian.tutorial.spring.helloworld; public interface HelloWorld { public void sayHello(); }
- HelloWorldService.java
package com.zaixian.tutorial.spring.helloworld; public class HelloWorldService { private HelloWorld helloWorld; public HelloWorldService() { } public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } public HelloWorld getHelloWorld() { return this.helloWorld; } }
- SpringHelloWorld.java
package com.zaixian.tutorial.spring.helloworld.impl; import com.zaixian.tutorial.spring.helloworld.HelloWorld; public class SpringHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Spring Say Hello!!"); } }
- StrutsHelloWorld.java
package com.zaixian.tutorial.spring.helloworld.impl; import com.zaixian.tutorial.spring.helloworld.HelloWorld; public class StrutsHelloWorld implements HelloWorld { @Override public void sayHello() { System.out.println("Struts Say Hello!!"); } }
- HelloProgram.java
package com.zaixian.tutorial.spring; import com.zaixian.tutorial.spring.helloworld.HelloWorld; import com.zaixian.tutorial.spring.helloworld.HelloWorldService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloProgram { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld hw= service.getHelloWorld(); hw.sayHello(); } }
-
beans.xml
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <beanid="springHelloWorld" class="com.zaixian.tutorial.spring.helloworld.impl.SpringHelloWorld"></bean> <beanid="strutsHelloWorld" class="com.zaixian.tutorial.spring.helloworld.impl.StrutsHelloWorld"></bean> <beanid="helloWorldService" class="com.zaixian.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="springHelloWorld"/> </bean> </beans>
6- 運行示例
運行 HelloProgram.java

運行 HelloProgram 類的結果如下:


打開 beans.xml 檔並更改配置:
<!-- Original --> <beanid="helloWorldService" class="com.zaixian.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="springHelloWorld"/> </bean> <!-- Change to: --> <beanid="helloWorldService" class="com.zaixian.tutorial.spring.helloworld.HelloWorldService"> <propertyname="helloWorld"ref="strutsHelloWorld"/> </bean>
重新運行 HelloProgram 類並得到以下結果。


7- Spring的工作原理
Spring在這個例子中,工作原理說明如下圖所示:


beans.xml
-
這是一個配置檔,您可以在這裏聲明Java bean。
可以通過讀取beans.xml 檔來創建一個應用程式上下文對象
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml");
在這個例子中,HelloWorldService 是一個 java bean 注入依賴。
<!-- beans.xml --> <beanid="helloWorldService" class="com.zaixian.tutorial.spring.helloworld.HelloWorldService"> <!-- Call: helloWorldService.setHelloWorld(springHelloWorld) --> <propertyname="helloWorld"ref="springHelloWorld"/> </bean>
8- 使用Spring MVC - 編寫Web應用程式
接下來,你可以學習使用Spring MVC編寫Web應用程式:
9- 附:下載Spring 庫
您可以下載 Spring 從以下網址:

解壓下載的zip檔到硬碟驅動器檔夾,如下:

上一篇:
Spring依賴注入servlet會話監聽器
下一篇:
安裝Spring工具套件到Eclipse