1 ProxyFactoryBean proxy 的創建交給 AopProxy 去做。

public ? class ?ProxyFactoryBean? extends ?AdvisedSupport?

????
implements ?FactoryBean,?BeanFactoryAware,?AdvisedSupportListener? {?

????????????
public ?Object?getObject()? throws ?BeansException? {?

????????????????????????
// invoke?AopProxy?去?創建?proxy?
????????????????????????
return ? this .singleton? ? ?getSingletonInstance()?:?newPrototypeInstance();?? [good code]
????????????}
?

????????????
// inherited?from?ProxyConfig?
???????????
public ? void ?setAopProxyFactory(AopProxyFactory?apf)? {?

????????????????????????
this .aopProxyFactory? = ?apf;?
????????????}
?

????????????
// inherited?from?ProxyConfig?

????????????
public ?AopProxyFactory?getAopProxyFactory()? {?
????????????????????????
return ? this .aopProxyFactory;?
????????????}
?

}
?

?

2. 、那么 AopProxy 又如何獲取?答案是從 AopProxyFactory 獲得。這個 AopProxyFactory 已在 ProxyFactoryBean 掌控范圍內,如上述代碼所示。 AopProxyFactory interface 如下所示:

?

public ? interface ?AopProxyFactory? {????
????????????AopProxy?createAopProxy(AdvisedSupport?advisedSupport)?
throws ?AopConfigException;
}
?


3AopProxy是如何introduce Advised interface的?那要先從AopProxyFactoryimplementation說起。

AopProxyFactory 的實現類目前只有 DefaultAopProxyFactory 。這個類的核心方法如下:

???????????

? public ?AopProxy?createAopProxy(AdvisedSupport?advisedSupport)? throws ?AopConfigException? {?

????????????????????????
if ?(advisedSupport.isOptimize()? || ?advisedSupport.isProxyTargetClass()? || ?

????????????????????????????advisedSupport.getProxiedInterfaces().length?
== ? 0 )? {?

????????????????????????????????????
if ?( ! cglibAvailable)? {?

????????????????????????????????????????????????
throw ? new ?AopConfigException(?

????????????????????????????????????????????????????????????????????????
" Cannot?proxy?target?class?because?CGLIB2?is?not?available.? " ? + ?

????????????????????????????????????????????????????????????????????????
" Add?CGLIB?to?the?class?path?or?specify?proxy?interfaces. " );?
????????????????????????????????}
?

????????????????????????????????????
return ?CglibProxyFactory.createCglibProxy(advisedSupport);?
??????????????????????}
?

????????????????????????
else ? {?

????????????????????????????????????
return ? new ?JdkDynamicAopProxy(advisedSupport);?
??????????????????????}
?

????????????}
?

?? 這個方法主要是依賴 CglibProxyFactory 類和 JdkDynamicAopProxy 類。這兩個類是如何代理所有的接口的呢?

這兩個類都是重要接口 AopProxy 的實現類,這個接口的核心方法是

??????????? public Object getProxy(ClassLoader classLoader)

其中 JdkDynamicAopProxy 實現該方法用到的獲取需要代理的接口的代碼如下:

??????????

?? public ?Object?getProxy(ClassLoader?classLoader)? {?

????????????????????????
if ?(logger.isDebugEnabled())? {?

????????????????????????????????????Class?targetClass?
= ? this .advised.getTargetSource().getTargetClass();?

????????????????????????????????????logger.debug(
" Creating?JDK?dynamic?proxy " ? + ?

????????????????????????????????????????????????????????????(targetClass?
!= ? null ? ? ? " ?for?[ " ? + ?targetClass.getName()? + ? " ] " ?:? "" ));?
????????????}
?

????????????????????????Class[]?proxiedInterfaces?
= ?AopProxyUtils.completeProxiedInterfaces( this .advised);?

????????????????????????
return ?Proxy.newProxyInstance(classLoader,?proxiedInterfaces,? this );?
????????????}
?

?

CglibProxyFactory getProxy(ClassLoader classLoader) 方法如下:

??????????

?? public ?Object?getProxy(ClassLoader?classLoader)? {?

……?

????????????????????????Enhancer?enhancer?
= ? new ?Enhancer();????????????????????????

……?

????????????enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(
this .advised));?

……?

}
?

?

從上面可以看出,這兩個 AopProxy 實現方案在獲取接口時都是通過 AopProxyUtils.completeProxiedInterfaces(this.advised) 獲取的。該方法如下:

??????????? /**

??????????? ?* Get complete set of interfaces to proxy. This will always add the Advised interface

??????????? ?* unless the AdvisedSupport's "opaque" flag is true.

??????????? ?* @return the complete set of interfaces to proxy

??????????? ?*/

???????????

? public ? static ?Class[]?completeProxiedInterfaces(AdvisedSupport?advised)? {?

????????????????????????
// ?Won't?include?Advised,?which?may?be?necessary.?

????????????????????????Class[]?specifiedInterfaces?
= ?advised.getProxiedInterfaces();?

????????????????????????Class[]?proxiedInterfaces?
= ?specifiedInterfaces;?

????????????????????????
if ?( ! advised.isOpaque()? && ? ! advised.isInterfaceProxied(Advised. class ))? {?

????????????????????????????????????
// ?We?need?to?add?the?Advised?interface.?

????????????????????????????????????proxiedInterfaces?
= ? new ?Class[specifiedInterfaces.length? + ? 1 ];?

????????????????????????????????????proxiedInterfaces[
0 ]? = ?Advised. class ;?

????????????????????????????????????System.arraycopy(specifiedInterfaces,?
0 ,?proxiedInterfaces,? 1 ,?specifiedInterfaces.length);?

??????????????????????}
?

????????????????????????
return ?proxiedInterfaces;?

????????????}
?

從這可以看出,是在這強制加了對 Advised? interface 的實現。前提是是否允許代理該 interafce ,這由 isOpaque 方法確定,該方法 Return whether proxies created by this configuration should be prevented from being cast to Advised.

?

4. 順便提一下, Advised interface 的具體實現又是怎樣的,在哪呢?

4.1 JdkDynamicAopProxy 里,

?

??????????? public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

……

??????????????????????? ??????????? if (Advised.class == method.getDeclaringClass()) {

??????????????????????? ??????????? ??????????? // service invocations on ProxyConfig with the proxy config

??????????????????????? ??????????? ??????????? return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);

??????????????????????? ??????????? }

……

}

注意第一個參數是 this.advised 其實就是 ProxyFactoryBean 本身,注意在我發表的其天的 Spring 文章里也曾提到, ProxyFactoryBean extends AdvisedSupport

AdisedSupport extends ProxyConfig implements Advised ,也就是說 Advised 的默認實現就是 AdisedSupport

?

4.2 Cglib2AopProxy 里(如果不熟 Cglib ,建議先看看其天有關的 Cglib 資料),

?

?

??????????? public Object getProxy(ClassLoader classLoader) {

……

Enhancer enhancer = new Enhancer();

……

enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised));

……

Callback[] callbacks = getCallbacks(rootClass);

……

……

……

}

private Callback[] getCallbacks(Class rootClass) throws Exception {

……

??????????? ??????????? Callback[] mainCallbacks = new Callback[]{

??????????????????????? ??????????? aopInterceptor, // for normal advice

??????????????????????? ??????????? targetInterceptor, // invoke target without considering advice, if optimized

??????????????????????? ??????????? new SerializableNoOp(), // no override for methods mapped to this

??????????????????????? ??????????? targetDispatcher, this.advisedDispatcher,

??????????????????????? ??????????? new EqualsInterceptor(this.advised)

??????????? ??????????? };

……

}

?

private class ProxyCallbackFilter implements CallbackFilter {

public int accept(Method method) {

??????????????????????? ??????????? if (method.getDeclaringClass() == Advised.class) {

??????????????????????? ??????????? ??????????? if (logger.isDebugEnabled()) {

??????????????????????? ??????????????????????? ??????????? logger.debug("Method " + method + " is declared on Advised - using DISPATCH_ADVISED");

??????????????????????? ????????????????????? }

??????????????????????? ??????????? ??????????? return DISPATCH_ADVISED;// DISPATCH_ADVISED 值為 4.

??????????????????????? ??????????? }

……

}

}

?

private final transient AdvisedDispatcher advisedDispatcher = new AdvisedDispatcher();

?

??????????? /**

??????????? ?* Dispatcher for any methods declared on the Advised class.

??????????? ?*/

??????????? private class AdvisedDispatcher implements Dispatcher, Serializable {

?

??????????? ??????????? public Object loadObject() throws Exception {

??????????????????????? ??????????? return advised;

????????????????????? }

??????????? }

?

注意 Dispatcher extends Callback interface