分享
 
 
 

Spring ProxyFactoryBean 是如何实现所有的AOP proxy都有可能转为Advised接口的?

王朝other·作者佚名  2007-01-25
窄屏简体版  字體: |||超大  

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();

}

//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;

}

3、AopProxy是如何introduce Advised interface的?那要先从AopProxyFactory的implementation说起。

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 。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有