簡單設計動態代理,基本模擬spring的動態代理方式。
before afterReturning around afterException after這些通知方法都可以這塊模擬出來
spring的AOP:
1.在容器中的對象如果實現了接口則采用JDK的動態代理。
2在容器中的對象沒有實現接口,則用(cglib)繼承的方式實現動態代理。
現在模擬spring的動態代理。
首先准備接口(UserService)和實現接口的目標對象(UserServiceImpl)。
1 public interface UserService { 2 3 void save(); 4 5 }
1 public class UserServiceImpl implements UserService { 2 3 public void save() { 4 System.out.println("保存用戶"); 5 } 6 7 }
1.動態代理
1 /** 2 * 動態代理1 3 * 4 * @author shihaibin 5 * @param <T> 6 * 7 */ 8 public class UserServiceProxyFactory implements InvocationHandler { 9 10 Object impl; 11 12 public <T> Object getProxy(Class<T> clz) { 13 try { 14 impl = clz.newInstance(); 15 } catch (InstantiationException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } catch (IllegalAccessException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 // 生成動態代理 23 Object usProxy = Proxy.newProxyInstance(clz.getClassLoader(), clz.getInterfaces(), this); 24 // 返回 25 return usProxy; 26 } 27 28 /** 29 * 參數:1.當前代理對象 2.當前方法 3.當前方法執行的時候的參數 30 */ 31 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 32 before(); 33 Object invoke = method.invoke(impl, args); 34 after(); 35 return invoke; 36 } 37 //可以重寫 38 public void before() { 39 System.out.println("之后"); 40 } 41 42 public void after() { 43 System.out.println("之后"); 44 } 45 }
測試:
1 @Test 2 public void find1() { 3 // 簡單的aop 4 UserServiceProxyFactory factory = new UserServiceProxyFactory(); 5 UserService userServiceProxy = (UserService) factory.getProxy(UserServiceImpl.class); 6 userServiceProxy.save(); 7 System.out.println(userServiceProxy instanceof UserServiceImpl); 8 }
2.cglib代理
1 /** 2 * 動態代理2 cglib代理 3 * 4 * @author shihaibin 5 * 6 */ 7 public class UserServiceProxyFactory2 implements MethodInterceptor { 8 9 public <T> Object getProxy(Class<T> clz) { 10 Enhancer en = new Enhancer();// 幫我們生成代理對象 11 en.setSuperclass(clz);// 設置對誰進行代理 12 en.setCallback(this);//回調函數 13 return en.create();// 創建代理對象; 14 } 15 16 /** 17 * prxoyobj:被代理的原始對象 method:被代理的原始方法 arg:運行期的參數 methodProxy:產生的代理方法 18 */ 19 public Object intercept(Object prxoyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable { 20 // 打開事務 21 System.out.println("打開事務!"); 22 // 調用原有方法 23 Object invokeSuper = methodProxy.invokeSuper(prxoyobj, arg); 24 // 提交事務 25 System.out.println("提交事務!"); 26 return invokeSuper; 27 } 28 }
測試:
1 @Test 2 public void find2() { 3 // 重寫aop前后方法 4 UserServiceProxyFactory2 factoryContext = new UserServiceProxyFactory2(); 5 UserService userServiceProxy = (UserService) factoryContext.getProxy(UserServiceImpl.class); 6 userServiceProxy.save(); 7 // 判斷代理對象是否屬於被代理對象類型 8 // 代理對象繼承了被代理對象=>true 9 System.out.println(userServiceProxy instanceof UserServiceImpl);// 判斷是否屬於被代理對象類型 10 }