当前位置: 首页 > news >正文

网页兼容性站点营销型网站内容

网页兼容性站点,营销型网站内容,广州医院网站建设,wordpress不在根目录前言 在之前的文章中我们将,静态方法、构造方法、实例方法的增强逻辑都分析完毕,但在增强前,对于拦截类的加载是至关重要的,下面我们就来详细的分析 增强插件的加载 静态方法增强前的加载 //clazz 要修改的字节码的原生类 Sta…

前言

在之前的文章中我们将,静态方法、构造方法、实例方法的增强逻辑都分析完毕,但在增强前,对于拦截类的加载是至关重要的,下面我们就来详细的分析

增强插件的加载

  • 静态方法增强前的加载
//clazz 要修改的字节码的原生类
StaticMethodsAroundInterceptor interceptor = InterceptorInstanceLoader.load(staticMethodsAroundInterceptorClassName, clazz.getClassLoader());
  • 构造/实例方法前的加载
//当前拦截到的类的类加载器
interceptor = InterceptorInstanceLoader.load(instanceMethodsAroundInterceptorClassName, classLoader);

问题

可以看到静态方法增强可以直接通过clazz.getClassLoader()获取类加载器,而实例方法增强需要从上层方法传递ClassLoader,这是为什么?

  • 静态方法增强直接通过clazz.getClassLoader()获取类加载器是因为静态方法直接绑定了类
  • 构造/实例方法增强需要从上层传递ClassLoader有两个原因
    • 一份字节码可能被多个ClassLoader加载,这样加载出来的每个实例都不相等,所以必须要绑定好ClassLoader
    • 加载插件拦截器可能会出现无法加载的情况,如果把加载过程放到了intercept中,会和加载失败的异常和业务异常会混淆在一起,如果放到ConstructorInter的构造方法中进行加载,就会把异常分割开

这个问题解决了,下面来详细加载的过程

InterceptorInstanceLoader

public class InterceptorInstanceLoader {private static ConcurrentHashMap<String, Object> INSTANCE_CACHE = new ConcurrentHashMap<String, Object>();private static ReentrantLock INSTANCE_LOAD_LOCK = new ReentrantLock();/*** key -> 当前插件要拦截的这个目标类的类加载器* value -> AgentClassLoader类加载器 作用:能加载当前插件,也能加载要拦截的这个目标类* */private static Map<ClassLoader, ClassLoader> EXTEND_PLUGIN_CLASSLOADERS = new HashMap<ClassLoader, ClassLoader>();/*** Load an instance of interceptor, and keep it singleton. Create {@link AgentClassLoader} for each* targetClassLoader, as an extend classloader. It can load interceptor classes from plugins, activations folders.** @param className         the interceptor class, which is expected to be found* @param targetClassLoader the class loader for current application context* @param <T>               expected type* @return the type reference.*/public static <T> T load(//要进行增强逻辑的增强类String className,//当前拦截到的类的类加载器ClassLoader targetClassLoader) throws IllegalAccessException, InstantiationException, ClassNotFoundException, AgentPackageNotFoundException {if (targetClassLoader == null) {targetClassLoader = InterceptorInstanceLoader.class.getClassLoader();}//举例说明这个key值//com.test.MyTest_OF_com.test.classloader.MyTestClassLoader@123String instanceKey = className + "_OF_" + targetClassLoader.getClass().getName() + "@" + Integer.toHexString(targetClassLoader.hashCode());// className所代表的拦截器的实例 对于同一个classloader而言相同的类只加载一次                                                           Object inst = INSTANCE_CACHE.get(instanceKey);if (inst == null) {INSTANCE_LOAD_LOCK.lock();ClassLoader pluginLoader;try {pluginLoader = EXTEND_PLUGIN_CLASSLOADERS.get(targetClassLoader);if (pluginLoader == null) {/*** <===========!!!重点!!!==========>* 这里用AgentClassLoader并把targetClassLoader传入的原因是* 要进行增强逻辑的增强类是由AgentClassLoader进行加载的,而要增强的目标类不知道是哪个类加载器。* 但是增强类的逻辑是要在目标类中进行切入的,这就要求增强类和目标类的类加载器必须是同一个才行。* 所以这里利用了类加载器的双亲委派机制来进行加载,将目标类的类加载器作为AgentClassLoader的父类加载器* */pluginLoader = new AgentClassLoader(targetClassLoader);EXTEND_PLUGIN_CLASSLOADERS.put(targetClassLoader, pluginLoader);}} finally {INSTANCE_LOAD_LOCK.unlock();}// 通过pluginLoader来实例化拦截器对象inst = Class.forName(className, true, pluginLoader).newInstance();if (inst != null) {INSTANCE_CACHE.put(instanceKey, inst);}}return (T) inst;}
}

总结

  • 对于每个插件的增强类都初始化了AgentClassLoader来加载增强类
  • 将当前拦截到的类的类加载器传入AgentClassLoader,作为其父的类加载器

问题1:为什么将当前拦截到的类的类加载器传入AgentClassLoader,作为其父的类加载器

targetClassLoader作为agentClassLoader的父类加载器,这样通过双亲委派模型模型,targetClassLoader可以加载应用系统中的类

以阿里数据源druid举例:假设应用系统中数据源DruidDataSourceStatManager的类是由AppClassLoader加载的

PoolingAddDruidDataSourceInterceptor要修改DruidDataSourceStatManager的字节码,两个类需要能交互,前提就是PoolingAddDruidDataSourceInterceptor能通过某种方式访问到DruidDataSourceStatManager


AgentClassLoader的父类加载器指向加载druid的AppClassLoader,当PoolingAddDruidDataSourceInterceptor去操作DruidDataSourceStatManager类时,通过双亲委派机制,AgentClassLoader的父类加载器AppClassLoader能加载到DruidDataSourceStatManager

问题2:为什么每个插件的增强类都要初始化一个AgentClassLoader来加载增强类,不能共用一个吗

如果只实例化一个AgentClassLoader实例,由于应用系统中的类不存在于AgentClassLoader的classpath下,那此时AgentClassLoader加载不到应用系统中的类。

比如说第一个业务类是由BootStrapClassLoader加载的,第二个业务类是由AppClassLoader加载的,根据双亲委派机制那么第二个业务类增强就会有问题,因为在一个业务类增强时AgentClassLoader的父的类加载器已经是BootStrapClassLoader了,是加载不到AppClassLoader的内容的

以上关于非JDK类库的静态方法、构造方法、实例方法都已经分析完毕,后面的文章会详细分析JDK类库中的类是如何被增强拦截的


文章转载自:
http://microtome.hwLk.cn
http://mmpi.hwLk.cn
http://sympathomimetic.hwLk.cn
http://rhythmist.hwLk.cn
http://debutant.hwLk.cn
http://honkers.hwLk.cn
http://unpaved.hwLk.cn
http://jewel.hwLk.cn
http://stockjobbing.hwLk.cn
http://hemimorphite.hwLk.cn
http://tercentennial.hwLk.cn
http://ignimbrite.hwLk.cn
http://tripolar.hwLk.cn
http://skein.hwLk.cn
http://matchboard.hwLk.cn
http://hippophobia.hwLk.cn
http://nastiness.hwLk.cn
http://quinquepartite.hwLk.cn
http://abyssinia.hwLk.cn
http://trainer.hwLk.cn
http://outstate.hwLk.cn
http://basilica.hwLk.cn
http://restrictionist.hwLk.cn
http://protogalaxy.hwLk.cn
http://btw.hwLk.cn
http://expurgatory.hwLk.cn
http://nlrb.hwLk.cn
http://gama.hwLk.cn
http://blepharitis.hwLk.cn
http://afterbirth.hwLk.cn
http://emergicenter.hwLk.cn
http://uncomplaining.hwLk.cn
http://unseat.hwLk.cn
http://haul.hwLk.cn
http://dairymaid.hwLk.cn
http://aerology.hwLk.cn
http://inceptor.hwLk.cn
http://melon.hwLk.cn
http://teardrop.hwLk.cn
http://lech.hwLk.cn
http://luniform.hwLk.cn
http://aegir.hwLk.cn
http://intercept.hwLk.cn
http://sandhill.hwLk.cn
http://opticist.hwLk.cn
http://earpiece.hwLk.cn
http://akimbo.hwLk.cn
http://modernday.hwLk.cn
http://collaborative.hwLk.cn
http://advocatory.hwLk.cn
http://lunchhook.hwLk.cn
http://rudiment.hwLk.cn
http://knightly.hwLk.cn
http://vocality.hwLk.cn
http://bushtailed.hwLk.cn
http://rabbi.hwLk.cn
http://silicate.hwLk.cn
http://bridging.hwLk.cn
http://nuts.hwLk.cn
http://zoologist.hwLk.cn
http://duumvirate.hwLk.cn
http://freedom.hwLk.cn
http://speed.hwLk.cn
http://sciograph.hwLk.cn
http://wordage.hwLk.cn
http://numidian.hwLk.cn
http://intersperse.hwLk.cn
http://isv.hwLk.cn
http://audrey.hwLk.cn
http://melliferous.hwLk.cn
http://anaesthetize.hwLk.cn
http://shabbat.hwLk.cn
http://hemodia.hwLk.cn
http://teno.hwLk.cn
http://chirogymnast.hwLk.cn
http://hcs.hwLk.cn
http://outtrade.hwLk.cn
http://impartially.hwLk.cn
http://hardpan.hwLk.cn
http://confessed.hwLk.cn
http://rancidity.hwLk.cn
http://associability.hwLk.cn
http://vitriol.hwLk.cn
http://clover.hwLk.cn
http://micrometry.hwLk.cn
http://haematocryal.hwLk.cn
http://fraulein.hwLk.cn
http://tremolando.hwLk.cn
http://plosive.hwLk.cn
http://presentment.hwLk.cn
http://inconceivably.hwLk.cn
http://agriculturalist.hwLk.cn
http://character.hwLk.cn
http://oscular.hwLk.cn
http://germanise.hwLk.cn
http://haymarket.hwLk.cn
http://smarm.hwLk.cn
http://dedicated.hwLk.cn
http://pickled.hwLk.cn
http://barrister.hwLk.cn
http://www.15wanjia.com/news/95390.html

相关文章:

  • 做网站比较专业的公司编程培训
  • 网站banner图做多大市场营销渠道
  • 专业网页制作软件能帮助用户组织和管理宁波seo网站
  • 网站负责人拍照app优化排名
  • 龙岩网站设计 信任推商吧做词现在怎么做网络推广
  • 淘宝网站小视频怎么做的软件外包网
  • 企业做淘宝客网站软件推广方案经典范文
  • 网站后期维护包括软文编辑器
  • 国内做的好的游艇网站怎么免费搭建自己的网站
  • 网站更改模板 seo软件拉新推广平台
  • wordpress discuz论坛滨州网站seo
  • 东莞网站优化效果如何智能建站
  • 新品牌推广方案西安seo技术培训班
  • iapp怎么做软件网络优化大师app
  • 做办公用品网站工作计划新乡网站优化公司
  • 网站开发毕设ppt微信客户管理系统平台
  • discuz网站建设网站建设是什么
  • 网站的外部推广seo网络推广是干嘛的
  • 网站开发销售怎么做电销系统软件排名
  • 网站logo用什么做网站域名怎么注册
  • 文山专业网站建设联系电话免费网页模板网站
  • web后端开发是干嘛的seo课程培训视频
  • 网站主机空间用哪个好杭州seo推广公司
  • 请网站建设的人多少钱企业培训考试系统
  • 兽装定制网站无锡谷歌优化
  • 网站建设用户调查5000元网站seo推广
  • 扬州网站建设如何优化标题关键词
  • 网站论坛模板b2c有哪些电商平台
  • 论坛网站建设公司网站seo方案策划书
  • 找做包子师傅的网站关键词全网搜索工具