今天課程:Spring架構第一天
Spring架構的學習路線
1. Spring第一天:Spring的IOC容器之XML的方式,Spring架構與Web項目整合
2. Spring第二天:Spring的IOC容器之注解的方式,Spring的AOP技術
3. Spring第三天:Spring的事務管理、Spring架構的JDBC模闆
4. Spring第四天:SSH三大架構的整合
今天内容概述
1. Spring架構的概述
2. SpringIOC的快速入門
3. IoC容器XML的方式
4. 在web項目中內建Spring
案例一:使用Spring的IOC技術完成客戶的儲存功能
需求分析
1. 使用Spring的IOC技術完成客戶的儲存功能
技術分析之Spring架構的概述和入門
技術分析之什麼是Spring架構
1. Spring架構的概述
* Spring是一個開源架構
* Spring是于2003 年興起的一個輕量級的Java開發架構,由Rod Johnson在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。
* 它是為了解決企業應用開發的複雜性而建立的。架構的主要優勢之一就是其分層架構,分層架構允許使用者選擇使用哪一個元件,同時為 J2EE 應用程式開發提供內建的架構。
* Spring使用基本的JavaBean來完成以前隻可能由EJB完成的事情。然而,Spring的用途不僅限于伺服器端的開發。從簡單性、可測試性和松耦合的角度而言,任何Java應用都可以 從Spring中受益。
* Spring的核心是控制反轉(IoC)和面向切面(AOP)。簡單來說,Spring是一個分層的JavaSE/EEfull-stack(一站式) 輕量級開源架構。
* EE開發分成三層結構
* WEB層 -- Spring MVC
* 業務層 -- Bean管理:(IOC)
* 持久層 -- Spring的JDBC模闆.ORM模闆用于整合其他的持久層架構
技術分析之Spring架構的特點
1. 為什麼要學習Spring的架構
* 友善解耦,簡化開發
* Spring就是一個大工廠,可以将所有對象建立和依賴關系維護,交給Spring管理
* AOP程式設計的支援
* Spring提供面向切面程式設計,可以友善的實作對程式進行權限攔截、運作監控等功能
* 聲明式事務的支援
* 隻需要通過配置就可以完成對事務的管理,而無需手動程式設計
* 友善程式的測試
* Spring對Junit4支援,可以通過注解友善的測試Spring程式
* 友善內建各種優秀架構
* Spring不排斥各種優秀的開源架構,其内部提供了對各種優秀架構(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支援
* 降低JavaEE API的使用難度
* Spring 對JavaEE開發中非常難用的一些API(JDBC、JavaMail、遠端調用等),都提供了封裝,使這些API應用難度大大降低
2. Spring架構的版本
* Spring3.x和Spring4.x的版本
技術分析之Spring架構的IOC核心功能快速入門
1. 什麼是IOC的功能?
* IoC -- Inverse of Control,控制反轉,将對象的建立權反轉給Spring!!
* 使用IOC可以解決的程式耦合性高的問題!!

2. 步驟一:下載下傳Spring架構的開發包
* 官網:http://spring.io/
* 下載下傳位址:http://repo.springsource.org/libs-release-local/org/springframework/spring解壓:(Spring目錄結構:)
* docs -- API和開發規範
* libs -- jar包和源碼
* schema -- 限制
3. 步驟二:建立JavaWEB項目,引入Spring的開發包
* 引入Spring架構IOC核心功能需要的具體的jar包
* Spring架構的IOC的功能,那麼根據Spring架構的體系結構圖能看到,隻需要引入如下的jar包
* Beans
* Core
* Context
* Expression Language
* Spring架構也需要引入日志相關的jar包
* 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
* com.springsource.org.apache.commons.logging-1.1.1.jar
* 還需要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15
* com.springsource.org.apache.log4j-1.2.15.jar
4. 步驟三:建立對應的包結構,編寫Java的類,要注意:以後使用Spring架構做開發,都需要來編寫接口與實作類!!
* com.itcast.demo1
* UserService -- 接口
* UserServiceImpl -- 具體的實作類
5. 步驟四:想把UserServiceImpl實作類的建立交給Spring架構來管理,需要建立Spring架構的配置檔案,完成配置
* 在src目錄下建立applicationContext.xml的配置檔案,名稱是可以任意的,但是一般都會使用預設名稱!!
* 引入spring的限制,需要先找到具體的限制頭資訊!!
* spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html
* 具體的限制如下:
<beans xmlns="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">
</beans>
* 完成UserService的配置
<!-- Spring的快速入門 -->
<bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>
6. 步驟五:編寫測試程式,采用Spring架構的工廠方式來擷取到UserService接口的具體實作類!!
public void demo2(){
// 使用Spring的工廠:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通過工廠獲得類:
UserService userService = (UserService) applicationContext.getBean("userService");
userService.sayHello();
}
入門總結之Spring架構中的工廠(了解)
1. ApplicationContext接口
* 使用ApplicationContext工廠的接口,使用該接口可以擷取到具體的Bean對象
* 該接口下有兩個具體的實作類
* ClassPathXmlApplicationContext -- 加載類路徑下的Spring配置檔案
* FileSystemXmlApplicationContext -- 加載本地磁盤下的Spring配置檔案
2. BeanFactory工廠(是Spring架構早期的建立Bean對象的工廠接口)
* 使用BeanFactory接口也可以擷取到Bean對象
public void run(){
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserService us = (UserService) factory.getBean("us");
us.sayHello();
}
* BeanFactory和ApplicationContext的差別
* BeanFactory -- BeanFactory采取延遲加載,第一次getBean時才會初始化Bean
* ApplicationContext -- 在加載applicationContext.xml時候就會建立具體的Bean對象的執行個體,還提供了一些其他的功能
* 事件傳遞
* Bean自動裝配
* 各種不同應用層的Context實作
入門總結之配置Spring架構編寫XML的提示
1. 步驟一:先複制, http://www.springframework.org/schema/beans/spring-beans.xsd
2. 步驟二:搜尋XML Catalog,點選Add按鈕
3. 步驟三:先選擇Location的schema的限制位址
* E:\class\2016\JavaEE28\day35_Spring架構第一天\資料\spring-framework-4.2.4.RELEASE-schema\beans\spring-beans-4.2.xsd
4. 步驟四:注意:Key type要選擇:Schema location
5. 步驟五:Key把http://www.springframework.org/schema/beans/spring-beans.xsd複制上
技術分析之Spring架構的Bean管理的配置檔案方式
技術分析之Spring架構中标簽的配置
1. id屬性和name屬性的差別
* id -- Bean起個名字,在限制中采用ID的限制,唯一
* 取值要求:必須以字母開始,可以使用字母、數字、連字元、下劃線、句話、冒号 id:不能出現特殊字元
* name -- Bean起個名字,沒有采用ID的限制(了解)
* 取值要求:name:出現特殊字元.如果<bean>沒有id的話 , name可以當做id使用
* Spring架構在整合Struts1的架構的時候,Struts1的架構的通路路徑是以/開頭的,例如:/bookAction
2. class屬性 -- Bean對象的全路徑
3. scope屬性 -- scope屬性代表Bean的作用範圍
* singleton -- 單例(預設值)
* prototype -- 多例,在Spring架構整合Struts2架構的時候,Action類也需要交給Spring做管理,配置把Action類配置成多例!!
* request -- 應用在Web項目中,每次HTTP請求都會建立一個新的Bean(少用)
* session -- 應用在Web項目中,同一個HTTP Session 共享一個Bean(少用)
* globalsession -- 應用在Web項目中,多伺服器間的session
4. Bean對象的建立和銷毀的兩個屬性配置(了解)
* 說明:Spring初始化bean或銷毀bean時,有時需要作一些處理工作,是以spring可以在建立和拆卸bean的時候調用bean的兩個生命周期方法
* init-method -- 當bean被載入到容器的時候調用init-method屬性指定的方法
* destroy-method -- 當bean從容器中删除的時候調用destroy-method屬性指定的方法
* 想檢視destroy-method的效果,有如下條件
* scope= singleton有效
* web容器中會自動調用,但是main函數或測試用例需要手動調用(需要使用ClassPathXmlApplicationContext的close()方法)
技術分析之依賴注入(DI)
1. IOC和DI的概念
* IOC -- Inverse of Control,控制反轉,将對象的建立權反轉給Spring!!
* DI -- Dependency Injection,依賴注入,在Spring架構負責建立Bean對象時,動态的将依賴對象注入到Bean元件中!!
應用:eg:service需要DAO,但是都交給spring管理了,不準自己new了,怎麼辦必須要用dao呀,此時需要依賴注入
2. DI(依賴注入)
* 例如:如果UserServiceImpl的實作類中有一個屬性,那麼使用Spring架構的IOC功能時,可以通過依賴注入把該屬性的值傳入進來!!
* 具體的配置如下
<bean id="us" class="com.itheima.demo1.UserServiceImpl">
<property name="uname" value="小風"/>
</bean>
技術分析之Spring架構的屬性注入
1. 對于類成員變量,常用的注入方式有兩種
* 構造函數注入
* 屬性setter方法注入
2. 在Spring架構中提供了前兩種的屬性注入的方式
1. 構造方法的注入方式,兩步
* 編寫Java的類,提供構造方法
public class Car {
private String name;
private double money;
public Car(String name, double money) {
this.name = name;
this.money = money;
}
@Override
public String toString() {
return "Car [name=" + name + ", money=" + money + "]";
}
}
* 編寫配置檔案
<bean id="car" class="com.itheima.demo4.Car">
<constructor-arg name="name" value="大奔"/>
<constructor-arg name="money" value="100"/>
</bean>
2. 屬性的setter方法的注入方式(标準的注入方式)
* 編寫Java的類,提供屬性和對應的set方法即可
* 編寫配置檔案
3. 如果Java類的屬性是另一個Java的類,那麼需要怎麼來注入值呢?
* <property name="name" rel="具體的Bean的ID或者name的值"/>
* 例如:
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="美美"/>
<property name="car2" ref="car2"/>
</bean>
技術分析之Spring的2.5版本中提供了一種:p名稱空間的注入(了解)
1. 步驟一:需要先引入 p 名稱空間
* 在schema的名稱空間中加入該行:xmlns:p="http://www.springframework.org/schema/p"
2. 步驟二:使用p名稱空間的文法
* p:屬性名 = ""
* p:屬性名-ref = ""
3. 步驟三:測試
* <bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
技術分析之Spring的3.0提供了一種:SpEL注入方式(了解)
1. SpEL:Spring Expression Language是Spring的表達式語言,有一些自己的文法
2. 文法
* #{SpEL}
3. 例如如下的代碼
<!-- SpEL的方式 -->
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="#{'小風'}"/>
<property name="car2" value="#{car2}"/>
</bean>
4. 還支援調用類中的屬性或者方法
* 定義類和方法,例如
public class CarInfo {
public String getCarname(){
return "奇瑞QQ";
}
}
技術分析之數組,集合(List,Set,Map),Properties等的注入(自己寫的不多,但整合時要用)
1. 如果是數組或者List集合,注入配置檔案的方式是一樣的
<bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
<property name="arrs">
<list>
<value>美美</value>
<value>小風</value>
</list>
</property>
</bean>
2. 如果是Set集合,注入的配置檔案方式如下:
<property name="sets">
<set>
<value>哈哈</value>
<value>呵呵</value>
</set>
</property>
3. 如果是Map集合,注入的配置方式如下:
<property name="map">
<map>
<entry key="老王2" value="38"/>
<entry key="鳳姐" value="38"/>
<entry key="如花" value="29"/>
</map>
</property>
4. 如果是properties屬性檔案的方式,注入的配置如下:
<property name="pro">
<props>
<prop key="uname">root</prop>
<prop key="pass">123</prop>
</props>
</property>
技術分析之Spring架構的配置檔案分開管理(了解)
1. 例如:在src的目錄下又多建立了一個配置檔案,現在是兩個核心的配置檔案,那麼加載這兩個配置檔案的方式有兩種!
* 主配置檔案中包含其他的配置檔案:
<import resource="applicationContext2.xml"/>
* 工廠建立的時候直接加載多個配置檔案:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml","applicationContext2.xml");
Spring架構整合WEB
Spring架構整合WEB(不是最終的方案)
1. 建立JavaWEB項目,引入Spring的開發包。編寫具體的類和方法。
* 環境搭建好後,啟動伺服器來測試項目,發送每通路一次都會加載一次配置檔案,這樣效率會非常非常慢!!
2. 解決上面的問題
* 将工廠建立好了以後放入到ServletContext域中.使用工廠的時候,從ServletContext中獲得.
* ServletContextListener:用來監聽ServletContext對象的建立和銷毀的監聽器.
* 當ServletContext對象建立的時候:建立工廠 , 将工廠存入到ServletContext
* [ServletContext類(最大的全局共享域 隻有一個)]
3. Spring整合Web項目
* 引入spring-web-4.2.4.RELEASE.jar包
* 配置監聽器
<!-- 配置Spring的核心監聽器: -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- src目錄下的檔案一編譯就到WEB-INF/Class下面去了 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
4. 修改servlet的代碼
* 從ServletContext中獲得工廠
* 具體代碼如下
ServletContext servletContext = ServletActionContext.getServletContext();
// 需要使用WEB的工廠的方式
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.save();
代碼整合