AOP基礎(chǔ) - 動(dòng)態(tài)代理
1). 創(chuàng)建一個(gè)接口:Counter.java
public interface Counter {
public int add(int a, int b);
public int sub(int a, int b);
}
2). 創(chuàng)建這個(gè)接口的實(shí)現(xiàn)類:CounterImp.java
public class CounterImp implements Counter {
public int add(int a, int b){
//加法核心代碼
return a + b;
}
public int sub(int a, int b){
//減法核心代碼
return a - b;
}
}
3). 創(chuàng)建一個(gè)代理類:CounterProxy.java
public class CounterProxy {
//要代理的對(duì)象
private CounterProxy target;
//構(gòu)造器凭需,用于給 target 賦值
public CounterProxy(Counter target){
this.target = target;
}
//獲取代理
public Counter getProxy(){
Counter proxy = null;
//代理對(duì)象由哪一個(gè)類加載器負(fù)責(zé)加載
ClassLoader loader = target.getClass().getClassLoader();
//代理對(duì)象的類型谭溉,即其中有哪些方法
Class [] interfaces = new Class[]{Counter.class};
//當(dāng)調(diào)用代理對(duì)象其中的方法時(shí)富拗,該執(zhí)行的代碼
InvocationHandler h = new InvocationHandler(){
/**
* proxy: 正在返回的那個(gè)代理對(duì)象,一般情況下出革,在 invoke 方法中都不使用該對(duì)象,會(huì)無限循環(huán)
* method: 正在被調(diào)用的方法
* args: 調(diào)用方法時(shí)物邑,傳入的參數(shù)
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwble{
String methodName = method.getName();
//日志
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
//執(zhí)行方法
Object result = method.invoke(target, args);
//日志
System.out.println("The method " + methodName + " ends with " + result);
return result;
}
}
proxy = (Counter) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
4). 創(chuàng)建一個(gè)測(cè)試類:Main.java
public class Main{
public static void main(String[] args){
Counter target = new CounterImp();
Counter proxy = new CounterProxy(target).getProxy();
int result = proxy.add(1, 2);
System.out.println("-->" + result);
result = proxy.sub(2, 1);
System.out.println("-->" + result);
}
}
5). 運(yùn)行結(jié)果:
The method add begins with [1, 2]
The method add ends with 3
-->3
The method sub begins with [2, 1]
The method sub ends with 1
-->1
Spring AOP
1. 通知
Spring 的 AOP 包括六種通知類型:前置通知想罕,后置通知,返回通知圈澈,異常通知i惫周,環(huán)繞通知,以及引用通知(不常用)康栈。
- 前置通知:在方法執(zhí)行前
- 后置通知:在方法執(zhí)行后递递,不管有沒有異常
- 返回通知:在方法正常結(jié)束后
- 異常通知:在方法拋出異常時(shí)
- 環(huán)繞通知:類似動(dòng)態(tài)代理全過程,可以自定義切面位置
2.配置方式
1). 注解方式:略
2). 基于 Spring 配置文件:applicationContext.xml
i. 創(chuàng)建一個(gè) 日志切面:LogginAspect.java
public class LoggingAspect{
//前置通知
public void beforeMethod(JoinPoint joinPoint){
String methodName = jointPoint.getSinature().getName();
Object [] args = jointPoint.getArgs();
System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
}
//返回通知
public void afterReturning(JoinPoint joinPoint, Object result){
String methodName = jointPoint.getSinature().getName();
System.out.println("The method " + methodName + " ends with " + result);
}
}
ii. 配置 applicationContext.xml
<!-- 配置 bean -->
<bean id="counter"
class="package.CounterImp"></bean>
<!-- 配置切面的 bean -->
<bean id="loggingAspect"
class="package.LoggingAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切點(diǎn)表達(dá)式 -->
<aop:pointcut expression="execution(* package.*(..))"
id="pointcut" />
<!-- 配置切面及通知啥么,order 屬性為切面的優(yōu)先級(jí)登舞,數(shù)字越小,優(yōu)先級(jí)越高 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" />
</aop:aspect>
</aop:config>
iii. 創(chuàng)建一個(gè)測(cè)試類:Main.java
public class Main{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Counter counter = (Counter) ctx.getBean("counter");
System.out.pritln(counter.getClass().getName());
int result = counter.add(1, 2);
System.out.println("-->" + result);
result = counter.sub(2, 1);
System.out.println("-->" + result);
}
}
iiii.運(yùn)行結(jié)果
$Proxy3
The method add begins with [1, 2]
The method add ends with 3
-->3
The method sub begins with [2, 1]
The method sub ends with 1
-->1