spring之AOP的簡單一實例,springaop

來源:互聯網
上載者:User

spring之AOP的簡單一實例,springaop

AOP:面向切面編程,就是把除去業務部分以外的東西單獨模組化,比如打日誌等,就像學生資訊的增刪改查,可以把輸出日誌單獨模組化出來,通過切面對的方式進行編程。

在進行執行個體編寫之前先進行一些專業術語的瞭解

切面aspect:是對象操作過程中的截面,是一段代碼,實現額外的模組化功能

連接點join point:是指對象操作過程中的某個階段的點,連接點實際上是對象的一個操作

切入點pointcut:切入點是連接點的集合,切面與程式的交叉點就是程式的切入點。也就是切面注入到程式中的位置。換句話說,切面就是通過切入點被注入的。

通知advice:通知是摸個切入點被橫切後,所採取的處理邏輯。在切入點處攔截程式後,通過通知來執行切面。

目標對象target:所有被通知的對象(也可以理解為被代理的對象)都是目標對象。目標對象被AOP所關注,隨時準備向目標對象注入切面

織入Weaving:織入是將切面功能應用到目標對象的過程。由代理工廠建立一個代理對象,這個代理對象可以為目標對象執行切面功能,就是這個代理對象是根據目標對象織入切面後產生的。

執行個體的實現如下所示:

1.匯入需要的jar包-這些是基本的包(可以看到整體結構)

 

 

2.編寫執行個體類,Student類和需要增加額外功能的列印檔案StudentPrint類代碼如下所示:

 1 package com.gp.service; 2  3 public class Student { 4    public void addStudent(){ 5        System.out.println("------新增學生資訊-------"); 6    } 7    public void updateStudent(){ 8        System.out.println("------修改學生資訊-------"); 9    }10    public void deleteStudent(){11        System.out.println("------刪除學生資訊-------");12    }13    public void selectStudent(){14        System.out.println("------查詢學生資訊-------");15    }16 }

 

 1 package com.gp.service; 2  3 public class StudentPrint { 4    public void before(){ 5        System.out.println("執行學生功能前的準備工作----開始執行"); 6    } 7    public void after(){ 8        System.out.println("學生功能實現後的列印工作----功能已實現,此處進行列印工作"); 9    }10 }

 

3.實體類完成後,下面進行設定檔的編寫:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4        xmlns:aop="http://www.springframework.org/schema/aop" 5        xsi:schemaLocation="http://www.springframework.org/schema/beans 6            http://www.springframework.org/schema/beans/spring-beans.xsd 7            http://www.springframework.org/schema/aop 8            http://www.springframework.org/schema/aop/spring-aop.xsd" > 9 10   11    <bean id="student" class="com.gp.service.Student"/>12    <bean id="myStudent" class="com.gp.service.StudentPrint"/>13    <aop:config>14       <aop:pointcut expression="execution(* com.gp.service.Student.*(..))" id="pointcut1"/>15       <aop:aspect ref="myStudent">16          <aop:before method="before" pointcut-ref="pointcut1" />17          <aop:after-returning method="after" pointcut-ref="pointcut1"/>18       </aop:aspect>19    </aop:config>20   21 </beans>

 

其中的部分說明:運算式中表示攔截Student類中的所有方法,唯一id為pointcut1,作為切入點,用作後面切面的切入

4.測試類別TestStudent類的檔案:

 

 1 package com.gp.test; 2  3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5  6 import com.gp.service.Student; 7  8 public class TestStudent { 9   public static void main(String[] args) {10      ApplicationContext ct=new ClassPathXmlApplicationContext("applicationContext.xml");11      Student student=(Student)ct.getBean("student");12      student.addStudent();13      student.deleteStudent();14      student.updateStudent();15      student.selectStudent();16   }17 }

 

5.可以進行測試回合了,結果如下所示:

在啟動並執行過程中遇到一些的問題,

1)JDK的版本和spring的版本問題,本人使用的是JDK1.6的,而spring使用5.0後的時,測試檔案編譯不通過,

ApplicationContext ct=new ClassPathXmlApplicationContext("applicationContext.xml");

這行會提示報錯

2)jar包匯入的不全時,aopalliance.jar包沒有在下載的spring的jar包中,沒有匯入,結果運行時報如下錯誤:

java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice,匯入aopalliance.jar的jar包還不能解決,需匯入一下兩個包:

aopalliance.jar ,aspectjweaver-1.7.4.jar即可

到此,AOP的簡單一實例就完成了,下面會附上相應的jar包的連結

https://pan.baidu.com/s/1b5IGRj4syjZoKFSjL7aRrw

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.