2023-01-18
一、AOP前奏-代理模式
1、手動實作動態代理環境搭建
(1)基于介面實作動態代理:JDK動態代理
(2)基于繼承實作動態代理:Cglib、javassist動態代理
2、實作動態代理的步驟
(1)一個類:Proxy
①概述:Proxy代理類的基類(類似于Object)
②作用:newProxyInstance():創建代理物件
(2)一個介面:InvocationHandler
①概述:實作“動態織入效果”的關鍵介面
②作用:invoke(),執行invoke()實作動態織入效果
3、手動實作動態代理關鍵步驟
注意:代理物件與實作類(目標物件)是“兄弟”關系,不能相互轉換
(1)創建類(為實作創建代理物件工具類)
(2)提供屬性(目標物件:實作類)
(3)提供方法(創建代理物件)
(4)提供有參構造器(避免目標為空)
4、實體代碼
(1)開啟組件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 開啟組件掃描--> <context:component-scan base-package="com.hh"></context:component-scan> </beans>
(2)介面
public interface Calc { /** * 加法 * @param a * @param b * @return */ int add(int a,int b); /** * 解放 * @param a * @param b * @return */ int sub(int a,int b); /** * 乘法 * @param a * @param b * @return */ int mul(int a,int b); /** *除法 * @param a * @param b * @return */ int div(int a,int b); }
(3)實作類
@Component public class CalcImpl implements Calc { @Override public int add(int a, int b) { int result = a + b; return result; } @Override public int sub(int a, int b) { int result = a - b; return result; } @Override public int mul(int a, int b) { int result = a*b; return result; } @Override public int div(int a, int b) { int result = a/b; return result; } }
(4)日志類
public class MyLogging { /** * 方法之前 */ public static void beforeMethod(String methodName,Object[] arg){ System.out.println("==>Calc中"+methodName+"方法(),引數:"+ Arrays.toString(arg)); } /** * 方法之后 */ public static void afterMethod(String methodName,Object rs){ System.out.println("==>Calc中"+methodName+"方法(),結果:"+rs); } }
(5)代理類
public class MyProxy { /** * 目標物件(目標客戶) */ private Object target; public MyProxy(Object target){ this.target = target; } /** * 獲取目標物件的代理物件 * @return */ public Object getProxyObject(){ Object proxyObj = null; /* ClassLoader loader:類加載器,目標物件類加載器 Class<?>[] interfaces,目標物件實作介面,目標物件實作所有介面 InvocationHandler h */ ClassLoader classLoader = target.getClass().getClassLoader(); Class<?>[] interfaces = target.getClass().getInterfaces(); //創建代理物件 proxyObj = Proxy.newProxyInstance(classLoader, interfaces, new InvocationHandler() { //執行invoke()實作動態織入效果 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //獲取方法名(目標物件) String methodName = method.getName(); //執行目標方法之前,添加日志 MyLogging.beforeMethod(methodName,args); //觸發目標方法 Object rs = method.invoke(target, args); //執行目標方法之后,添加日志 MyLogging.afterMethod(methodName,rs); return rs; } }); return proxyObj; } }
(6)測驗類
@ContextConfiguration(locations = "classpath:applicationContext_beforeAop.xml") @RunWith(SpringJUnit4ClassRunner.class) public class TestBeforeAop { @Autowired private Calc calc; @Test public void testBeforeAop(){ int add = calc.add(1,2); System.out.println("add = " + add); } @Test public void testBeforeAop2(){ //目標物件 Calc calc = new CalcImpl(); //代理工具類 MyProxy myProxy = new MyProxy(calc); //獲取代理物件 Calc calcProxy = (Calc)myProxy.getProxyObject(); //測驗 int add = calcProxy.add(1, 2); int div = calcProxy.div(2, 1); System.out.println("add = " + add); System.out.println("div = " + div); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542192.html
標籤:其他
上一篇:lambda運算式
