spring源碼學習系列1.1-ProxyFactory AopProxy與Proxy之間的關係

1.ProxyFactory持有生成代理的相關資源,如advice targetSource等屬性(資源)。ProxyFactory是ProxyConfig及AdvisorSupport的子類,這些屬性在AdvisorSupport中。

在ProxyFactory中委託AopProxyFactory生成AopProxy,並將自身傳給AopProxy

[color=red]可以將ProxyFactory當做現實中的某個工廠。工廠中有各種各樣的原材料,在這裏,這些原材料就是目標類,目標類所實現的接口,攔截器等等。目標類或者接口可以想象成要加工的東西,而攔截器等就是一些輔助材料,用來加工最終成型的產品。
對於調用這來說,他只要提供材料就行或最終得到產品就行,不需要關心這些材料在一起怎麼生成產品的[/color]

例如:ProxyFactoryHelloWorld.java

2.AopProxy通過AopProxyFacotory生成,AopProxy持有以上ProxyFactory的對象,也即擁有了生成代理的相關資源。引用已有的某個對象的方法,可以繼承某個對象或者持有某個對象,這個用了第二種方式即引用。

[color=red]DefaultAopProxyFactory可以看做一個更大的工廠,包括了ProxyFactory這個小工廠或小作坊中的一些原材料(AdvisorSupport)。DefaultAopProxyFactory並不是ProxyFactory的子類,而是通過引用擁有了這個小工廠或者通過引用(AdvisorSupport)從而擁有了ProxyFactory相關的原材料[/color]

[color=red]DefaultAopProxyFactory中僅僅通過new JdkDynamicAopProxy(config);或者CglibProxyFactory.createCglibProxy(config);來創建AopProxy對象。依據是ProxyFactory中的某些屬性或者原材料[/color]

例如:AopProxyHelloWorld.java

3.AopProxy中繼續通過Proxy生成最終代理,最終代理中持有AopProxy對象。

AopProxy:Delegate interface for a configured AOP proxy, allowing for the creation of actual proxy objects

[color=red]AopProxy纔可以最終生成實際的代理對象,當然DefaultAopProxyFactory這個工廠製造AopProxy這個產品時,也將ProxyFactory這個小作坊(AdvisorSupport)給了AopProxy或者AopProxy可以使用ProxyFactory的原材料。[/color]

以JdkDynamicAopProxy爲例,其實現了InvocationHandler接口

例如:ProxyHelloWorld.java


綜上:
[color=red]原材料其實保存在ProxyFactory的父類AdvisedSupport中。ProxyFactory,AopProxyFactory,AopProxy通過不同方式共享這個AdvisedSupport中的原材料。AopProxy只是一個半成品,實際的代理對象中包括了這個半成品AopProxy。調用代理對象時,也是通過半成品aopProxy來執行代理操作[/color]

[color=red]原材料的輸入是在後置處理器InfrastructureAdvisorAutoProxyCreator中產生的[/color]


ProxyFactoryHelloWorld
package com.bill.tx;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class ProxyFactoryHelloWorld {
public static void main(String[] args) {
ProxyFactory pf = new ProxyFactory();

pf.setTarget(new HelloWorldImpl());// 基本材料
pf.setInterfaces(HelloWorldImpl.class.getInterfaces());

pf.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before1");
}
});

pf.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before2");
}
});

pf.addAdvice(new AfterReturningAdvice() {

@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("afterReturning");
}
});

pf.addAdvice(new MethodInterceptor() {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("MethodInterceptor-start");
Object retValue = invocation.proceed();
System.out.println("MethodInterceptor-end");
return retValue;
}


});

HelloWorld hello = (HelloWorld) pf.getProxy(pf.getClass().getClassLoader());
String retValue = hello.sayHello("jpj");

System.out.println("==================輸出返回值");
System.out.println(retValue);
}
}



AopProxyHelloWorld
package org.springframework.aop.framework;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import com.bill.tx.HelloWorld;
import com.bill.tx.HelloWorldImpl;

public class AopProxyHelloWorld {
public static void main(String[] args) {
AdvisedSupport config = new AdvisedSupport(); // 原材料

config.setTarget(new HelloWorldImpl());
config.setInterfaces(HelloWorldImpl.class.getInterfaces());

config.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before1");
}
});

config.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before2");
}
});

config.addAdvice(new AfterReturningAdvice() {

@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("afterReturning");
}
});

config.addAdvice(new MethodInterceptor() {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("MethodInterceptor-start");
Object retValue = invocation.proceed();
System.out.println("MethodInterceptor-end");
return retValue;
}


});

AopProxy aopProxy = new JdkDynamicAopProxy(config);

HelloWorld hello = (HelloWorld) aopProxy.getProxy(aopProxy.getClass().getClassLoader());
String retValue = hello.sayHello("jpj");

System.out.println("==================輸出返回值");
System.out.println(retValue);
}
}



ProxyHelloWorld
package org.springframework.aop.framework;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import com.bill.tx.HelloWorld;
import com.bill.tx.HelloWorldImpl;

public class ProxyHelloWorld {
public static void main(String[] args) {
AdvisedSupport config = new AdvisedSupport(); // 原材料

config.setTarget(new HelloWorldImpl());
config.setInterfaces(HelloWorldImpl.class.getInterfaces());

config.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before1");
}
});

config.addAdvice(new MethodBeforeAdvice() {

@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("before2");
}
});

config.addAdvice(new AfterReturningAdvice() {

@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("afterReturning");
}
});

config.addAdvice(new MethodInterceptor() {

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("MethodInterceptor-start");
Object retValue = invocation.proceed();
System.out.println("MethodInterceptor-end");
return retValue;
}


});

JdkDynamicAopProxy aopProxy = new JdkDynamicAopProxy(config);

Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(config);
//findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
HelloWorld hello = (HelloWorld) Proxy.newProxyInstance(aopProxy.getClass().getClassLoader(), proxiedInterfaces, aopProxy);
String retValue = hello.sayHello("jpj");

System.out.println("==================輸出返回值");
System.out.println(retValue);
}
}


MyInvocationHandler:
package com.bill.tx;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyInvocationHandler implements InvocationHandler {
Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}

public static void main(String[] args) {
InvocationHandler myHandler = new MyInvocationHandler(new HelloWorldImpl());

HelloWorld hello = (HelloWorld) Proxy.newProxyInstance(myHandler.getClass().getClassLoader(), HelloWorldImpl.class.getInterfaces(), myHandler);
hello.sayHello("jpj");
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("MyInvocationHandler-start");
Object retValue = method.invoke(target, args);
System.out.println("MyInvocationHandler-end");
return retValue;
}


}



[img]http://dl2.iteye.com/upload/attachment/0123/9767/57552c82-82ef-3f94-9ea5-28ad455dcf60.png[/img]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章