JavaEE學習之Spring框架
一、Spring 簡介
-
簡介
-
Spring 是最受歡迎的企業級 Java 應用程式開發框架,數以百萬的來自世界各地的開發人員使用 Spring 框架來建立效能好、易於測試、可重用的程式碼。
-
Spring 框架是一個開源的 Java 平臺,它最初是由 Rod Johnson 編寫的,並且於 2003 年 6 月首次在 Apache 2.0 許可下發布。
-
Spring 是輕量級的框架,其基礎版本只有 2 MB 左右的大小。
-
Spring 框架的核心特性是可以用於開發任何 Java 應用程式,但是在 Java EE 平臺上構建 web 應用程式是需要擴充套件的。 Spring 框架的目標是使 J2EE 開發變得更容易使用,通過啟用基於 POJO 程式設計模型來促進良好的程式設計實踐。
-
三層架構
-
A 表現層 web層 MVC是表現層的一個設計模型
-
B 業務層 service層
-
C 持久層 dao層
二、Spring 體系結構
Spring 是模組化的,Spring 框架提供約 20 個模組,可以根據應用程式的要求來使用,允許你挑選和選擇適用於你的模組,不必要把剩餘部分也引入。
核心容器
核心容器由spring-core,spring-beans,spring-context,spring-context-support和spring-expression(SpEL,Spring表示式語言,Spring Expression Language)等模組組成,它們的細節如下:
-
spring-core模組提供了框架的基本組成部分,包括 IoC 和依賴注入功能。
-
spring-beans 模組提供 BeanFactory,工廠模式的微妙實現,它移除了編碼式單例的需要,並且可以把配置和依賴從實際編碼邏輯中解耦。
-
context模組建立在由core和 beans
-
spring-expression模組提供了強大的表示式語言,用於在執行時查詢和操作物件圖。它是JSP2.1規範中定義的統一表達式語言的擴充套件,支援set和get屬性值、屬性賦值、方法呼叫、訪問陣列集合及索引的內容、邏輯算術運算、命名變數、通過名字從Spring IoC容器檢索物件,還支援列表的投影、選擇以及聚合等。
它們的完整依賴關係如下圖所示:
資料訪問/整合
資料訪問/整合層包括 JDBC,ORM,OXM,JMS 和事務處理模組,它們的細節如下:
(注:JDBC=Java Data Base Connectivity,ORM=Object Relational Mapping,OXM=Object XML Mapping,JMS=Java Message Service)
-
JDBC 模組提供了JDBC抽象層,它消除了冗長的JDBC編碼和對資料庫供應商特定錯誤程式碼的解析。
-
ORM 模組提供了對流行的物件關係對映API的整合,包括JPA、JDO和Hibernate等。通過此模組可以讓這些ORM框架和spring的其它功能整合,比如前面提及的事務管理。
-
OXM 模組提供了對OXM實現的支援,比如JAXB、Castor、XML Beans、JiBX、XStream等。
-
JMS 模組包含生產(produce)和消費(consume)訊息的功能。從Spring 4.1開始,集成了spring-messaging模組。。
-
事務模組為實現特殊介面類及所有的 POJO 支援程式設計式和宣告式事務管理。(注:程式設計式事務需要自己寫beginTransaction()、commit()、rollback()等事務管理方法,宣告式事務是通過註解或配置由spring自動處理,程式設計式事務粒度更細)
Web
Web 層由 Web,Web-MVC,Web-Socket 和 Web-Portlet 組成,它們的細節如下:
-
Web 模組提供面向web的基本功能和麵向web的應用上下文,比如多部分(multipart)檔案上傳功能、使用Servlet監聽器初始化IoC容器等。它還包括HTTP客戶端以及Spring遠端呼叫中與web相關的部分。。
-
Web-MVC 模組為web應用提供了模型檢視控制(MVC)和REST Web服務的實現。Spring的MVC框架可以使領域模型程式碼和web表單完全地分離,且可以與Spring框架的其它所有功能進行整合。
-
Web-Socket 模組為 WebSocket-based 提供了支援,而且在 web 應用程式中提供了客戶端和伺服器端之間通訊的兩種方式。
-
Web-Portlet 模組提供了用於Portlet環境的MVC實現,並反映了spring-webmvc模組的功能。
其他
還有其他一些重要的模組,像 AOP,Aspects,Instrumentation,Web 和測試模組,它們的細節如下:
-
AOP 模組提供了面向方面的程式設計實現,允許你定義方法攔截器和切入點對程式碼進行乾淨地解耦,從而使實現功能的程式碼徹底的解耦出來。使用原始碼級的元資料,可以用類似於.Net屬性的方式合併行為資訊到程式碼中。
-
Aspects 模組提供了與 AspectJ 的整合,這是一個功能強大且成熟的面向切面程式設計(AOP)框架。
-
Instrumentation 模組在一定的應用伺服器中提供了類 instrumentation 的支援和類載入器的實現。
-
Messaging 模組為 STOMP 提供了支援作為在應用程式中 WebSocket 子協議的使用。它也支援一個註解程式設計模型,它是為了選路和處理來自 WebSocket 客戶端的 STOMP 資訊。
-
測試模組支援對具有 JUnit 或 TestNG 框架的 Spring 元件的測試。
三、Spring 環境配置
-
spring相關jar包
-
spring 核心配置檔案(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!--告訴spring幫我們建立物件-->
<bean name="user" class="cn.edu.dgut.pojo.User"></bean>
</beans> -
測試類(SpringDemo01)
public class SpringDemo01 {
四、Spring IOC(控制反轉)
-
Spring 容器是 Spring 框架的核心。容器將建立物件,把它們連線在一起,配置它們,並管理他們的整個生命週期從建立到銷燬。Spring 容器使用依賴注入(DI)來管理組成一個應用程式的元件。這些物件被稱為 Spring Beans。
-
IOC 容器具有依賴注入功能的容器,它可以建立物件,IOC 容器負責例項化、定位、配置應用程式中的物件及建立這些物件間的依賴。通常new一個例項,控制權由程式設計師控制,而"控制反轉"是指new例項工作不由程式設計師來做而是交給Spring容器來做。在Spring中BeanFactory是IOC容器的實際代表者。
-
Spring 的 BeanFactory 容器
這是一個最簡單的容器,它主要的功能是為依賴注入 (DI) 提供支援,這個容器介面在 org.springframework.beans.factory.BeanFactor 中被定義。BeanFactory 和相關的介面,比如BeanFactoryAware、DisposableBean、InitializingBean,仍舊保留在 Spring 中,主要目的是向後相容已經存在的和那些 Spring 整合在一起的第三方框架。
-
Spring ApplicationContext 容器
Application Context 是 BeanFactory 的子介面,也被成為 Spring 上下文。
Application Context 是 spring 中較高階的容器。和 BeanFactory 類似,它可以載入配置檔案中定義的 bean,將所有的 bean 集中在一起,當有請求的時候分配 bean。 另外,它增加了企業所需要的功能,比如,從屬性檔案中解析文字資訊和將事件傳遞給所指定的監聽器。這個容器在 org.springframework.context.ApplicationContext interface 介面中定義。
ApplicationContext 包含 BeanFactory 所有的功能,一般情況下,相對於 BeanFactory,ApplicationContext 會更加優秀。當然,BeanFactory 仍可以在輕量級應用中使用,比如移動裝置或者基於 applet 的應用程式。
-
Bean
被稱作 bean 的物件是構成應用程式的支柱也是由 Spring IoC 容器管理的。bean 是一個被例項化,組裝,並通過 Spring IoC 容器所管理的物件。
-
Bean 的作用域
當在 Spring 中定義一個 bean 時,你必須宣告該 bean 的作用域的選項。例如,為了強制 Spring 在每次需要時都產生一個新的 bean 例項,你應該宣告 bean 的作用域的屬性為 prototype。同理,如果你想讓 Spring 在每次需要時都返回同一個bean例項,你應該宣告 bean 的作用域的屬性為 singleton。
作用域 描述 singleton 在spring IoC容器僅存在一個Bean例項,Bean以單例方式存在,預設值 prototype 每次從容器中呼叫Bean時,都返回一個新的例項,即每次呼叫getBean()時,相當於執行newXxxBean() request 每次HTTP請求都會建立一個新的Bean,該作用域僅適用於WebApplicationContext環境 session 同一個HTTP Session共享一個Bean,不同Session使用不同的Bean,僅適用於WebApplicationContext環境 global-session 一般用於Portlet應用環境,該運用域僅適用於WebApplicationContext環境
五、Spring DI(依賴注入)
Spring框架的核心功能之一就是通過依賴注入的方式來管理Bean之間的依賴關係。
-
依賴注入(常用的為建構函式和set方法注入)
每個基於應用程式的 java 都有幾個物件,由這些物件一起工作來呈現出終端使用者所看到的工作的應用程式。當編寫一個複雜的 Java 應用程式時,應用程式類應該儘可能獨立於其他 Java 類來增加這些類重用的可能性,並且在做單元測試時,測試獨立於其他類的獨立性。依賴注入(或有時稱為佈線)有助於把這些類粘合在一起,同時保持他們獨立。
-
Spring 基於建構函式的依賴注入
當容器呼叫帶有一組引數的類建構函式時,基於建構函式的 DI 就完成了,其中每個引數代表一個對其他類的依賴。
-
Spring 基於設值函式的依賴注入
當容器呼叫一個無參的建構函式或一個無參的靜態 factory 方法來初始化你的 bean 後,通過容器在你的 bean 上呼叫設值函式,基於設值函式的 DI 就完成了。
-
P名稱空間注入
P名稱空間注入走的也是set方法, 官方目的是簡化set注入的property標籤的寫法。
-
Spel注入
Spring Expression Language(簡稱 SpEL)是一種功能強大的表示式語言、用於在執行時查詢和操作物件圖;語法上類似於 Unified EL,但提供了更多的特性,特別是方法呼叫和基本字串模板函式。SpEL 的誕生是為了給 Spring 社群提供一種能夠與 Spring 生態系統所有產品無縫對接,能提供一站式支援的表示式語言。
-
注入集合
你已經看到了如何使用 value 屬性來配置基本資料型別和在你的 bean 配置檔案中使用
<property>
標籤的 ref 屬性來配置物件引用。
<?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.xsd">
<!--set方法注入-->
<bean name="user" class="cn.edu.dgut.pojo.User">
<property name="id" value="1"></property>
<property name="username" value="房子"></property>
<property name="myPojo" ref="mypojo"></property>
<!--注入陣列-->
<property name="arr">
<array>
<value>張三</value>
<value>李四</value>
<value>王五</value>
</array>
</property>
<!--注入list集合-->
<property name="list">
<list>
<value>張三</value>
<value>李四</value>
<value>王五</value>
<ref bean="mypojo"></ref>
</list>
</property>
<!--注入map集合-->
<property name="map">
<map>
<entry key="name" value="張三"></entry>
<entry key="username" value="李四"></entry>
<entry key="uname" value="王五"></entry>
<entry key