Spring AOP的两种代理(二)

2015-11-12 23:05:55 · 作者: · 浏览: 30
ject[] args,
? ? ? ? ? ? MethodProxy methodproxy) throws Throwable {? ? ? ?
? ? ? ? Object result = null;? ?
? ? ? ? try {
? ? ? ? ? ? ? System. out .println( "前置处理开始 ..." );
? ? ? ? ? ? ? result = methodproxy.invoke( targetObject , args);//执行目标对象的方法
? ? ? ? ? ? ? System. out .println( "后置处理开始? ..." );
? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? System. out .println( " 异常处理 ..." );
? ? ? ? ? } finally {
? ? ? ? ? ? ? System. out .println( " 调用结束 ..." );
? ? ? ? ? }
? ? ? ? ? return result;
? ? ? }?
? }
测试类:


? public class TestCGLIBProxy {


? ? ? ? public static void main(String[] args) {
? ? ? ?
? ? ? ? ? //我们要代理的真实对象
? ? ? ? ? TestCGLIBServiceImpl testCGLIB = new TestCGLIBServiceImpl();
? ? ? ? ? CGLIBProxy CGLIBproxy = new CGLIBProxy();
? ? ? ? ? TestCGLIBServiceImpl testCGLIBProxy = (TestCGLIBServiceImpl) CGLIBproxy.createProxyInstance(testCGLIB);
? ? ? ? ? testCGLIBProxy.add();
? ? ? }
? }


结果图:



写在后面:spring AOP的两种代理实现代码就写到这,这里只是实现了,如果你要想真正明白,还得熟悉其中原理机制,比如反射机制,newProxyInstance(...),Enhancer()原理,invoke()原理等等。