Spring中AOP基礎
一、以一個例子引入
1、例子描述:實現一個加減乘除的計算器,要求有日誌,能給在程序執行期間追蹤正在發生的活動。
2、代碼示例:
ArithmeticCalculator接口:
package com.at.aop;
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);
}
ArithmeticCalculator接口的實現類ArithmeticCalculatorLoggingImpl
package com.at.aop;
public class ArithmeticCalculatorLoggingImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
System.out.println("add 方法輸入參數是["+i+","+j+"]");
int result = i + j;
System.out.println("add 方法輸出結果是"+result);
return result;
}
@Override
public int sub(int i, int j) {
System.out.println("sub 方法輸入參數是["+i+","+j+"]");
int result = i - j;
System.out.println("sub 方法輸出結果是"+result);
return result;
}
@Override
public int mul(int i, int j) {
System.out.println("mul 方法輸入參數是["+i+","+j+"]");
int result = i * j;
System.out.println("mul 方法輸出結果是"+result);
return result;
}
@Override
public int div(int i, int j) {
System.out.println("div 方法輸入參數是["+i+","+j+"]");
int result = i / j;
System.out.println("div 方法輸出結果是"+result);
return result;
}
}
測試函數
package com.at.aop;
public class TestArithmeticCalculator {
public static void main(String[] args) {
ArithmeticCalculator arithmeticCalculator = null;
arithmeticCalculator = new ArithmeticCalculatorLoggingImpl();
int result = arithmeticCalculator.add(1, 2);
System.out.println(result);
result = arithmeticCalculator.div(4, 2);
System.out.println(result);
}
}
add 方法輸入參數是[1,2]
add 方法輸出結果是3
3
div 方法輸入參數是[4,2]
div 方法輸出結果是2
2
3、例子總結
(1)代碼混亂:越來越多的非業務需求(日誌和驗證等)加入後, 原有的業務方法急劇膨脹。每個方法在處理核心邏輯的同時還必須兼顧其他多個關注點。
(2)代碼分散: 以日誌需求爲例, 只是爲了滿足這個單一需求, 就不得不在多個模塊(方法)裏多次重複相同的日誌代碼。如果日誌需求發生變化, 必須修改所有模塊。
二、使用動態代理方式解決冗餘代碼問題
將ArithmeticCalculator接口的實現類改爲ArithmeticCalculatorImpl:
package com.at.aop;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package com.at.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ArithmeticCalculatorLoggingProxy {
//要代理的對象
private ArithmeticCalculator target;
public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
super();
this.target = target;
}
public ArithmeticCalculator getLoggingProxy(){
ArithmeticCalculator proxy=null;
//代理對象由哪一個類加載器負責加載
ClassLoader loader = target.getClass().getClassLoader();
//代理對象的類型,也就是其中有哪些方法
Class [] interfaces = new Class[]{ArithmeticCalculator.class};
//當調用代理對象其中的方法時候,執行的代碼
InvocationHandler h = new InvocationHandler() {
/**
* proxy:正在返回的那個代理對象。一般情況下,在invoke方法中都不使用該對象。
* method:正在被調用的方法
* args:調用方法時候,傳入的參數
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
//日誌
System.out.println("使用代理方式的方法"+methodName+"輸入參數是"+Arrays.asList(args));
//執行方法
Object result = method.invoke(target, args);
//日誌
System.out.println("使用代理方式的方法"+methodName+"輸出結果是"+result);
return result;
}
};
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
測試函數
ArithmeticCalculator target=new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
int result = proxy.add(1, 2);
System.out.println(result);
result = proxy.div(4, 2);
System.out.println(result);
使用代理方式的方法add輸入參數是[1, 2]
使用代理方式的方法add輸出結果是3
3
使用代理方式的方法div輸入參數是[4, 2]
使用代理方式的方法div輸出結果是2
2
三、引入AOP
(一)AOP簡介
1、AOP(Aspect-Oriented Programming, 面向切面編程): 是一種新的方法論, 是對傳統 OOP(Object-Oriented Programming, 面向對象編程) 的補充。2、AOP 的主要編程對象是切面(aspect), 而切面模塊化橫切關注點。
3、在應用 AOP 編程時, 仍然需要定義公共功能, 但可以明確的定義這個功能在哪裏, 以什麼方式應用, 並且不必修改受影響的類。 這樣一來橫切關注點就被模塊化到特殊的對象(切面)裏。
4、AOP 的好處:
(1)每個事物邏輯位於一個位置, 代碼不分散, 便於維護和升級.
(2)業務模塊更簡潔, 只包含核心業務代碼。
(二)AOP圖解和相關術語描述
1、圖解上面計算器的例子2、相關術語
(1)切面(Aspect): 橫切關注點(跨越應用程序多個模塊的功能)被模塊化的特殊對象。
(2)通知(Advice): 切面必須要完成的工作。
(3)目標(Target): 被通知的對象。
(4)代理(Proxy): 向目標對象應用通知之後創建的對象。
(5)連接點(Joinpoint):程序執行的某個特定位置:如類某個方法調用前、調用後、方法拋出異常後等。連接點由兩個信息確定:方法表示的程序執行點;相對點表示的方位。例如 ArithmethicCalculator#add() 方法執行前的連接點,執行點爲 ArithmethicCalculator#add(); 方位爲該方法執行前的位置。
(6)切點(pointcut):每個類都擁有多個連接點:例如 ArithmethicCalculator 的所有方法實際上都是連接點,即連接點是程序類中客觀存在的事務。AOP 通過切點定位到特定的連接點。類比:連接點相當於數據庫中的記錄,切點相當於查詢條件。切點和連接點不是一對一的關係,一個切點匹配多個連接點,切點通過
org.springframework.aop.Pointcut 接口進行描述,它使用類和方法作爲連接點的查詢條件。
By luoyepiaoxue2014
微博地址:http://weibo.com/luoyepiaoxue2014 點擊打開鏈接