springAOP之ProxyFactory.docx
- 文档编号:24279466
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:22
- 大小:103.40KB
springAOP之ProxyFactory.docx
《springAOP之ProxyFactory.docx》由会员分享,可在线阅读,更多相关《springAOP之ProxyFactory.docx(22页珍藏版)》请在冰豆网上搜索。
springAOP之ProxyFactory
springAOP之ProxyFactory
ProxyFactory就springAOP创建代理的工厂,现在以此为入口进行aop源码的解读。
首先看ProxyFactory的结构图:
TargetClassAware
根据结构图,先看接口TargetClassAware,该接口是暴露代理的目标类的最小实现,该接口只有一个方法,
Class
>getTargetClass();
1
获取实现对象的目标类,当目标类未知,返回null。
接口Advised
接下来看它的子接口Advised,
接口说明:
由类持有一个AOP代理工厂配置的实现,配置包拦截器、其他的通知、顾问、代理的接口。
spring的任何AOP代理都能强转为此接口,这样就能够允许操作aop的通知。
接口方法如下:
//通知配置是否冻结,冻结后通知不能做改变
booleanisFrozen();
//是否代理目标类
booleanisProxyTargetClass();
//返回aop的代理接口,不包括目标类
Class
>[]getProxiedInterfaces();
booleanisInterfaceProxied(Class
>intf);
voidsetTargetSource(TargetSourcetargetSource);
TargetSourcegetTargetSource();
//通过aop框架,作为用AopContext可检索的ThreadLocal向外暴露
//当一个advisedobject需要调用另外一个advisedobject时有用。
voidsetExposeProxy(booleanexposeProxy);
booleanisExposeProxy();
//设置代理配置为提前过滤,因此它包括适当的advisors(通过匹配代理目标类)
voidsetPreFiltered(booleanpreFiltered);
booleanisPreFiltered();
Advisor[]getAdvisors();
voidaddAdvisor(Advisoradvisor)throwsAopConfigException;
voidaddAdvisor(intpos,Advisoradvisor)throwsAopConfigException;
booleanremoveAdvisor(Advisoradvisor);
voidremoveAdvisor(intindex)throwsAopConfigException;
intindexOf(Advisoradvisor);
booleanreplaceAdvisor(Advisora,Advisorb)throwsAopConfigException;
voidaddAdvice(Adviceadvice)throwsAopConfigException;
voidaddAdvice(intpos,Adviceadvice)throwsAopConfigException;
booleanremoveAdvice(Adviceadvice);
intindexOf(Adviceadvice);
StringtoProxyConfigString();
ProxyConfig类:
方便的超类对于创建代理的配置,确保所以的代理创建对象有一致的属性。
方法如下:
publicvoidsetProxyTargetClass(booleanproxyTargetClass){
this.proxyTargetClass=proxyTargetClass;
}
publicbooleanisProxyTargetClass(){
returnthis.proxyTargetClass;
}
publicvoidsetOptimize(booleanoptimize){
this.optimize=optimize;
}
publicbooleanisOptimize(){
returnthis.optimize;
}
publicvoidsetOpaque(booleanopaque){
this.opaque=opaque;
}
publicbooleanisOpaque(){
returnthis.opaque;
}
publicvoidsetExposeProxy(booleanexposeProxy){
this.exposeProxy=exposeProxy;
}
publicbooleanisExposeProxy(){
returnthis.exposeProxy;
}
publicvoidsetFrozen(booleanfrozen){
this.frozen=frozen;
}
publicbooleanisFrozen(){
returnthis.frozen;
}
publicvoidcopyFrom(ProxyConfigother){
Assert.notNull(other,"OtherProxyConfigobjectmustnotbenull");
this.proxyTargetClass=other.proxyTargetClass;
this.optimize=other.optimize;
this.exposeProxy=other.exposeProxy;
this.frozen=other.frozen;
this.opaque=other.opaque;
}
@Override
publicStringtoString(){
StringBuildersb=newStringBuilder();
sb.append("proxyTargetClass=").append(this.proxyTargetClass).append(";");
sb.append("optimize=").append(this.optimize).append(";");
sb.append("opaque=").append(this.opaque).append(";");
sb.append("exposeProxy=").append(this.exposeProxy).append(";");
sb.append("frozen=").append(this.frozen);
returnsb.toString();
}
发现其中有一些方法是为了实现Advised接口而准备的。
AdvisedSupport类:
aop代理配置管理的基类。
自身不是aop代理,但其子类是能够直接获取aop代理实例的常规工厂。
该类为子类管理创建Advices和Advisors利用便利,但它实际上没有实现代理的创建方法,由子类各自提供。
构造函数代码实现:
privatetransientMap
//
publicAdvisedSupport(){
initMethodCache();
}
publicAdvisedSupport(Class
>[]interfaces){
this();
setInterfaces(interfaces);
}
//默认初始化是创建32个map
privatevoidinitMethodCache(){
this.methodCache=newConcurrentHashMap
}
对于带接口数组参数的构造函数,设置先清空,后重新设置:
privateList >>interfaces=newArrayList >>(); publicvoidsetInterfaces(Class >...interfaces){ Assert.notNull(interfaces,"Interfacesmustnotbenull"); this.interfaces.clear(); for(Class >ifc: interfaces){ addInterface(ifc); } } publicvoidaddInterface(Class >intf){ Assert.notNull(intf,"Interfacemustnotbenull"); if(! intf.isInterface()){ thrownewIllegalArgumentException("["+intf.getName()+"]isnotaninterface"); } if(! this.interfaces.contains(intf)){ this.interfaces.add(intf); //当代理接口有更改,也同时清空methodCache adviceChanged(); } } protectedvoidadviceChanged(){ this.methodCache.clear(); } publicbooleanremoveInterface(Class >intf){ returnthis.interfaces.remove(intf); } @Override publicClass >[]getProxiedInterfaces(){ returnthis.interfaces.toArray(newClass >[this.interfaces.size()]); } //判断所有的目标类是否是接口 @Override publicbooleanisInterfaceProxied(Class >intf){ for(Class >proxyIntf: this.interfaces){ if(intf.isAssignableFrom(proxyIntf)){ returntrue; } } returnfalse; } 设置代理目标的方法: publicstaticfinalTargetSourceEMPTY_TARGET_SOURCE=EmptyTargetSource.INSTANCE; TargetSourcetargetSource=EMPTY_TARGET_SOURCE; //将根据给得的object创建SingletonTargetSource publicvoidsetTarget(Objecttarget){ setTargetSource(newSingletonTargetSource(target)); } //直接设置TargetSource相应的接口实现,若为空,返回EMPTY_TARGET_SOURCE @Override publicvoidsetTargetSource(TargetSourcetargetSource){ this.targetSource=(targetSource! =null? targetSource: EMPTY_TARGET_SOURCE); } //该代理目标类能够转为targetClass,该方法是前面两个方法的替代: 当我们想要一个没有可达TargetSource的代理类 publicvoidsetTargetClass(Class >targetClass){ this.targetSource=EmptyTargetSource.forClass(targetClass); } @Override publicClass >getTargetClass(){ returnthis.targetSource.getTargetClass(); } 设置是否预先过滤,默认为false: privatebooleanpreFiltered=false; @Override publicvoidsetPreFiltered(booleanpreFiltered){ this.preFiltered=preFiltered; } @Override publicbooleanisPreFiltered(){ returnthis.preFiltered; } 设置AdvisorChainFactory: AdvisorChainFactoryadvisorChainFactory=newDefaultAdvisorChainFactory(); publicvoidsetAdvisorChainFactory(AdvisorChainFactoryadvisorChainFactory){ Assert.notNull(advisorChainFactory,"AdvisorChainFactorymustnotbenull"); this.advisorChainFactory=advisorChainFactory; } publicAdvisorChainFactorygetAdvisorChainFactory(){ returnthis.advisorChainFactory; } 对于Advisor的操作: privateAdvisor[]advisorArray=newAdvisor[0]; //获取操作 protectedfinalList returnthis.advisors; } @Override publicfinalAdvisor[]getAdvisors(){ returnthis.advisorArray; } //添加操作 @Override publicvoidaddAdvisor(Advisoradvisor){ intpos=this.advisors.size(); addAdvisor(pos,advisor); } @Override publicvoidaddAdvisor(intpos,Advisoradvisor)throwsAopConfigException{ if(advisorinstanceofIntroductionAdvisor){ validateIntroductionAdvisor((IntroductionAdvisor)advisor); } addAdvisorInternal(pos,advisor); } privatevoidvalidateIntroductionAdvisor(IntroductionAdvisoradvisor){ advisor.validateInterfaces(); //Iftheadvisorpassedvalidation,wecanmakethechange. Class >[]ifcs=advisor.getInterfaces(); for(Class >ifc: ifcs){ addInterface(ifc); } } privatevoidaddAdvisorInternal(intpos,Advisoradvisor)throwsAopConfigException{ Assert.notNull(advisor,"Advisormustnotbenull"); if(isFrozen()){ thrownewAopConfigException("Cannotaddadvisor: Configurationisfrozen."); } if(pos>this.advisors.size()){ thrownewIllegalArgumentException( "Illegalposition"+pos+"inadvisorlistwithsize"+this.advisors.size()); } this.advisors.add(pos,advisor); updateAdvisorArray(); adviceChanged(); } protectedfinalvoidupdateAdvisorArray(){ this.advisorArray=this.advisors.toArray(newAdvisor[this.advisors.size()]); } //对于多个Advisor的添加 publicvoidaddAdvisors(Advisor...advisors){ addAdvisors(Arrays.asList(advisors)); } //对于Advisor集合的添加 publicvoidaddAdvisors(Collection if(isFrozen()){ thrownewAopConfigException("Cannotaddadvisor: Configurationisfrozen."); } if(! CollectionUtils.isEmpty(advisors)){ for(Advisoradvisor: advisors){ if(advisorinstanceofIntroductionAdvisor){ validateIntroductionAdvisor((IntroductionAdvisor)advisor); } Assert.notNull(advisor,"Advisormustnotbenull"); this.advisors.add(advisor); } updateAdvisorArray(); adviceChanged(); } } //删除操作 @Override publicbooleanremoveAdvisor(Advisoradvisor){ intindex=indexOf(advisor); if(index==-1){ returnfalse; } else{ removeAdvisor(index); returntrue; } } @Override publicintindexOf(Advisoradvisor){ Assert.notNull(advisor,"Advisormustnotbenull"); returnthis.advisors.indexOf(advisor); } @Override publicvoidremoveAdvisor(intindex)throwsAopConfigException{ if(isFrozen()){ thrownewAopConfigException("CannotremoveAdvisor: Configurationisfrozen."); } if(index<0||index>this.advisors.size()-1){ thrownewAopConfigException("Advisorindex"+index+"isoutofbounds: "+ "Thisconfigurationonlyhas"+this.advisors.size()+"advisors."); } Advisoradvisor=this.advisors.get(index); if(advisorinstanceofIntroductionAdvisor){ IntroductionAdvisoria=(IntroductionAdvisor)advisor; //Weneedtoremoveintroductioninterfaces. for(intj=0;j removeInterface(ia.getInterfaces()[j]); } } this.advisors.remove(index); updateAdvisorArray(); adviceChanged(); } //替换操作 @Override publicbooleanreplaceAdvisor(Advisora,Advisorb)throwsAopConfigException{ Assert.notNull(a,"Advisoramustnotbenull"); Assert.notNull(b,"Advisorbmustnotbenull"); intindex=indexOf(a); if(index==-1){ returnfalse; } removeAdvisor(index); addAdvisor(index,b); returntrue; } 对于Advice的操作: @Override publicvoidaddAdvice(Adviceadvice)throwsAopConfigException{ intpos=this.advisors.size(); addAdvice(pos,advice); } @Override publicvoidaddAdvice(intpos,Adviceadvice)throwsAopConfigException{ Asse
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- springAOP ProxyFactory