1
、
ProxyFactoryBean
把
proxy
的創(chuàng)建交給
AopProxy
去做。

































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







3、AopProxy是如何introduce Advised interface的?那要先從AopProxyFactory的implementation說(shuō)起。
AopProxyFactory
的實(shí)現(xiàn)類目前只有
DefaultAopProxyFactory
。這個(gè)類的核心方法如下:
???????????


































?? 這個(gè)方法主要是依賴
CglibProxyFactory
類和
JdkDynamicAopProxy
類。這兩個(gè)類是如何代理所有的接口的呢?
這兩個(gè)類都是重要接口
AopProxy
的實(shí)現(xiàn)類,這個(gè)接口的核心方法是
???????????
public Object getProxy(ClassLoader classLoader)
。
其中
JdkDynamicAopProxy
實(shí)現(xiàn)該方法用到的獲取需要代理的接口的代碼如下:
??????????





















?
而
CglibProxyFactory
的
getProxy(ClassLoader classLoader)
方法如下:
??????????

















?
從上面可以看出,這兩個(gè)
AopProxy
實(shí)現(xiàn)方案在獲取接口時(shí)都是通過(guò)
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
???????????
?*/
???????????





























從這可以看出,是在這強(qiáng)制加了對(duì)
Advised? interface
的實(shí)現(xiàn)。前提是是否允許代理該
interafce
,這由
isOpaque
方法確定,該方法
Return whether proxies created by this configuration should be prevented from being cast to Advised.
?
4.
順便提一下,
Advised interface
的具體實(shí)現(xiàn)又是怎樣的,在哪呢?
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);
???????????????????????
??????????? }
……
}
注意第一個(gè)參數(shù)是
this.advised
,
其實(shí)就是
ProxyFactoryBean
本身,注意在我發(fā)表的其天的
Spring
文章里也曾提到,
ProxyFactoryBean extends AdvisedSupport
,
而
AdisedSupport extends ProxyConfig implements Advised
,也就是說(shuō)
Advised
的默認(rèn)實(shí)現(xiàn)就是
AdisedSupport
。
?
4.2
在
Cglib2AopProxy
里(如果不熟
Cglib
,建議先看看其天有關(guān)的
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
。