Spring4的學習(二)之AOP編程,spring4aop
1. AOP 簡介
AOP(Aspect-Oriented Programming, 面向切面編程): 是一種新的方法論, 是對傳統 OOP(Object-Oriented Programming, 物件導向編程) 的補充.
AOP 的主要編程對象是切面(aspect), 而切面模組化橫切關注點.
在應用 AOP 編程時, 仍然需要定義公用功能, 但可以明確的定義這個功能在哪裡, 以什麼方式應用, 並且不必修改受影響的類. 這樣一來橫切關注點就被模組化到特殊的對象(切面)裡.
AOP 的好處:
---每個事物邏輯位於一個位置, 代碼不分散, 便於維護和升級
---業務模組更簡潔, 只包含核心業務代碼.
2. AOP的術語和圖形表達
切面(Aspect): 橫切關注點(跨越應用程式多個模組的功能)被模組化的特殊對象
通知(Advice): 切面必須要完成的工作
目標(Target): 被通知的對象
代理(Proxy): 向目標對象應用通知之後建立的對象
連接點(Joinpoint):程式執行的某個特定位置:如類某個方法調用前、調用後、方法拋出異常後等。連接點由兩個資訊確定:方法表示的程式執行點;相對點表示的方位。例如 ArithmethicCalculator#add() 方法執行前的連接點,執行點為 ArithmethicCalculator#add(); 方位為該方法執行前的位置
切點(pointcut):每個類都擁有多個連接點:例如 ArithmethicCalculator 的所有方法實際上都是連接點,即連接點是程式類中客觀存在的事務。AOP 通過切點定位到特定的連接點。類比:連接點相當於資料庫中的記錄,切點相當於查詢條件。切點和連接點不是一對一的關係,一個切點匹配多個連接點,切點通過 org.springframework.aop.Pointcut 介面進行描述,它使用類和方法作為連接點的查詢條件。
3.在spring4中如何使用AOP?
①首先我們要做的還是先匯入一系列架包。匯入完以後右擊buildpath引用下,如下:
②寫一個含有+-*%方法的介面,並寫好他的實作類別代碼如下:
1.ArithmeticCalculator(介面)
package com.icss.spring.aopimpl;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);} 2.實作類別,記得在實作類別上面加上註解交給spring來管理
package com.icss.spring.aopimpl;import org.springframework.stereotype.Component;@Component("arithmeticCalculator")public class ArithmeticCalculatorImpl implements ArithmeticCalculator {public int add(int i, int j) {int result = i + j;return result;}public int sub(int i, int j) {int result = i - j;return result;}public int mul(int i, int j) {int result = i * j;return result;}public int div(int i, int j) {int result = i / j;return result;}} 3.先寫一個main函數來測試下,此時直接輸出result=12,結果前後並沒有列印日誌記錄,我們需要做的就是要讓結果前後顯示日誌記錄。
package com.icss.spring.aopimpl;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {/** * @param args */public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml"); ArithmeticCalculator arithmeticCalculator= applicationContext.getBean(ArithmeticCalculator.class); int result = arithmeticCalculator.add(3, 9); System.out.println("result = "+result);}}
4.下面寫一個loggingAspect日誌切面(AOP編程)
package com.icss.spring.aopimpl;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Component@Aspectpublic class LoggingAspect { @Before("execution(public int com.icss.spring.aopimpl.ArithmeticCalculatorImpl.add(int , int ))") public void beforeMethod() { System.out.println("add方法執行之前列印出來");}}
@Before("execution(public int com.icss.spring.aopimpl.ArithmeticCalculatorImpl.add(int , int ))")
這句話的意思就是我們需要明確的指出,這個方法需要在哪個方法執行之前,也可以使用萬用字元*,來讓所有的方法之前都執行這個方法。
5.最後一步就是我們要配置下aop.xml檔案,讓他自動組建代理程式對象,執行main就ok了!特別注意xmls!!!上面的命名空間,千萬不要忘了加!不然沒用!!!!
<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.icss.spring.aopimpl"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
6.我們想要的結果如下:(這裡唯寫了前置通知的方法,後置通知大家可以自己試試!)
7.大家還可以在通知方法中聲明一個類型為 joinpoint的參數,然後就能訪問串連細節了,如方法名和參數值!代碼如下:
Spring AOP編程
spring設定檔需要這句話
<aop:aspectj-autoproxy/>
無法解析時還要加xml命名空間 參考這個
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xmlns:context="www.springframework.org/schema/context"
xmlns:aop="www.springframework.org/schema/aop"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans-2.5.xsd
www.springframework.org/schema/context
......餘下全文>>
spring的AOP切面編程在實際項目中用到什方面?舉幾個例子?
Hibernate事務控制
大型系統的日誌管理...
很多 只要需要用到切面編程的地方都可以使用
可以減少重複代碼的數量 提高編程效率