1. 程式人生 > >【Java】 Spring 框架初步學習總結(一)簡單實現 IoC 和 AOP

【Java】 Spring 框架初步學習總結(一)簡單實現 IoC 和 AOP

1.0 其中 表示 只需要 第一篇 否則 info fin pojo

  Spring 是一個開源的設計層面的輕量級框架,Spring 的好處網上有太多,這裏就不在贅述。

  IoC 控制反轉和 AOP 面向切面編程是 Spring 的兩個重要特性。

  IoC(Inversion of Control)控制反轉意思大概就是,原本需要我們手動創建的對象,現在交給 Spring 來創建和管理,我們只需要拿過來用就行了。

  有的地方會把 IoC 稱為 DI(Dependency Injection)依賴註入,大概是說我們所需要的對象,由 Spring 提供並註入到我們要使用的地方。

  AOP(Aspect Oriented Programming)面前向切面編程簡單的說,在不改動代碼的情況下,給代碼增加新的功能。

  概念講完,就該實踐了,首先要把 Spring 添加到項目中,有兩種方法。

  第一種方法是直接添加 MyEclipse 內置的 Spring。

  打開 MyEclipse,我用的是10.6版本,選中一個項目,單擊界面頂端的菜單欄的 MyEclipse,選中 Project Capabilities,再單擊 Add Spring Capabilities,就會彈出如下的窗體。

技術分享圖片

  Spring version 選擇 Spring 的版本,我這裏最高只能選擇 3.1 ,其他的可以先不用管,單擊 Next 繼續。

技術分享圖片

  Folder 是選擇 Spring 核心配置文件的存放路徑,默認是 src,不可以寫不存在的路徑,會出問題。

  File 是給 Spring 核心配置文件命名,默認是 applicationContext.xml,為了方便我改成了 app.xml。

  單擊 Finish 完成,我的項目的結構就變成了這樣。

技術分享圖片

  Spring 就添加到了我們的項目當中。

  第二種方法是從 官網下載 Spring 的 jar 包,手動添加到項目中。

  官方下載地址 http://repo.spring.io/release/org/springframework/spring/,選一個版本下載其中spring-framework-x.x.x.RELEASE-dist.zip 壓縮文件。

  壓縮包解壓出來,docs 文件夾裏是 Spring 的 api,schema 文件夾裏規定 Spring 的 xml 配置文件所需要的 xsd 文件,而 libs 文件夾裏所有 spring-xxx-x.x.x.RELEASE.jar 就是我們所需要的 jar 包。

  在項目中新建一個 lib 文件夾,把 Spring 的 jar 包都復制進去,全部選中右鍵 Add to Bulid Path 導入。

  因為我們是手動添加的 jar 包,所以還需要手動創建 Spring 的配置文件。

  在 src 目錄下創建 xml 文件命名為 app.xml,再將下面代碼復制到 app.xml文件中。

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3     xmlns="http://www.springframework.org/schema/beans"
4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5     xmlns:p="http://www.springframework.org/schema/p"
6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7 
8 </beans>

  這段代碼是使用 MyEclipse 添加 Spring 時自動創建的配置文件中復制出來的,如果用的不是 Spring 3.1,最好把第 6 行末尾的 3.1 改成對應的版本號,防止出現因為版本問題造成的錯誤。

  使用上述兩種方法將 Spring 添加到項目中後,就可以開始使用 Spring 了。

  先編寫兩個類供我們測試使用。

 1 /**
 2  * 人
 3  * @author BWM
 4  *
 5  */
 6 public class Person {
 7     private String name;
 8     private String context;
 9     public String getName() {
10         return name;
11     }
12     public void setName(String name) {
13         this.name = name;
14     }
15     public String getContext() {
16         return context;
17     }
18     public void setContext(String context) {
19         this.context = context;
20     }
21     public void say(){
22         System.out.println(name + "說:" + context);
23     }
24 }
25 /**
26  * 展示
27  * @author BWM
28  *
29  */
30 public class Show {
31     private Person person;
32     public Person getPerson() {
33         return person;
34     }
35     public void setPerson(Person person) {
36         this.person = person;
37     }
38     public void show(){
39         person.say();
40     }
41 }

  屬性的 seter 方法是必須要有的,有了這兩個以後,就可以在 app.xml 裏寫代碼了。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
 7 
 8     <bean id="person" class="cn.bwm.pojo.Person">
 9         <property name="name" value="小楓"></property>
10         <property name="context" value="樹葉的一生,只是為了歸根嗎?"></property>
11     </bean>
12     
13     <bean id="show" class="cn.bwm.pojo.Show">
14         <property name="person" ref="person"></property>
15     </bean>
16 
17 </beans>

  beans 標簽是 Spring 配置文件的根標簽,兩個 beans 之間就是我們發揮的地方。

  bean 標簽定義了 一個對象,id 屬性是唯一標識且不可重復,class 屬性表示這個對象的類型,需要填寫完整路徑。

  property 標簽表示對象的屬性,name 屬性填寫屬性名,value 為基本類型的值,ref 表示引用其他的 bean。需要有對應屬性的 seter 方法,否則會報錯。

  通過配置文件,Spring 就會知道我們需要幾個對象,該給對象的什麽屬性賦什麽值,就可以在我們需要的時候提供給我們。

  現在可以寫代碼測試一下。

1 public class Test {
2     public static void main(String[] args) {
3         ApplicationContext context = new ClassPathXmlApplicationContext("app.xml");
4         Show show = (Show)context.getBean("show");
5         show.show();
6     }
7 }

  ApplicationContext 是一個接口,負責讀取 Spring 配置文件,管理對象加載、生命,維護 Bean 對象與 Bean 對象之間的依賴關系,負責 Bean 的生命周期等。

  ClassPathXmlApplicaionContext 是 ApplicationContext 接口的實現類,用於從 classpath 路徑中讀取 Spring 配置文件。

  getBean 方法用於獲取 bean 對象,參數是 bean 對象的 id,返回的是 Object 類型的所以需要強制轉換一下。

  以上就是 IoC 的簡單使用。

  我們再來試試 AOP,為此我們需要一個工具類。

 1 /**
 2  * 增強工具
 3  * @author Administrator
 4  *
 5  */
 6 public class AdviceUtil {
 7     public void before(){
 8         System.out.println("這是前置增強");
 9     }
10     
11     public void afterReturning(){
12         System.out.println("這是後置增強");
13     }
14 }

  這個類定義了 before 和 afterReturning 兩個方法,我們就可以去寫配置文件了。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 8     http://www.springframework.org/schema/aop
 9     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
10 
11     <bean id="person" class="cn.bwm.pojo.Person">
12         <property name="name" value="小楓"></property>
13         <property name="context" value="樹葉的一生,只是為了歸根嗎?"></property>
14     </bean>
15     
16     <bean id="show" class="cn.bwm.pojo.Show">
17         <property name="person" ref="person"></property>
18     </bean>
19 
20     <bean id="util" class="cn.bwm.util.AdviceUtil"></bean>
21 
22     <aop:config>
23         <aop:pointcut expression="execution(* cn.bwm.pojo.Show.show())" id="pointcut"/>
24         <aop:aspect ref="util">
25             <aop:before method="before" pointcut-ref="pointcut"/>
26             <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
27         </aop:aspect>
28     </aop:config>
29 
30 </beans>

  這裏要註意,beans 標簽中多了 xmlns:aop 屬性以及 xsi:schemaLocation 裏對 AOP 的相關代碼。

  多了一個 id 為 util 的 bean,class 是我們剛才創建的類。

  aop:config 標簽,與 AOP 相關的配置都放在其中。

  aop:pointcut 聲明切面,表示在什麽地方。id 為唯一標識符,expression 配置切入點表達式,execution 聲明切入點,括號中是一個切點表達式,用來匹配符合條件的方法。

  public * show(..):* 表示匹配所有類型的返回值,..表示匹配所有參數類型個數和類型。

  public void *(..):* 表示匹配所有方法名。

  * cn.bwm.pojo.*.*(..):這個表達式匹配 cn.bwm.pojo 包下所有類的所有方法。

  * cn.bwm..*.*(..):這個表達式匹配 cn.bwm 包及其子包下的所有類的所有方法。

  aop:aspect 使用 ref 引用含有增強方法的 bean。

  aop:after 聲明前置增強,aop:after-renturning聲明後置增強,表示做什麽。method 屬性表示調用的增強方法,pointcut-ref 引用 pointcut 切入點。

  測試代碼可以不用改,直接運行,就能夠看到運行效果。

技術分享圖片

  以上就是簡單的 AOP 的實現,我們規定切面,指定要執行的增強處理,從而達到不改變原代碼添加功能的效果。

  實際上 AOP 並不只有 前置增強和後置增強兩種,還有異常拋出增強、最終增強和環繞增強,這裏先不詳細介紹。

  

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  Spring 的第一篇總結就結束了,希望對看到這裏的你有所幫助。

  如有問題,歡迎交流。

【Java】 Spring 框架初步學習總結(一)簡單實現 IoC 和 AOP