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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值