运行时的方面编织 [英] Aspect weaving at runtime

查看:70
本文介绍了运行时的方面编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种Java解决方案,该解决方案允许我使用AOP在运行时在已经运行的代码之上编织新代码.关键是不需要重新启动JVM.另外,我想在运行时删除编织,而使旧代码按编织前的方式运行.

I'm looking for a Java solution that would allow me to use AOP to weave new code on top of already running code at runtime. The key is not to require the restart of the JVM. Also, I'd like to remove the woven at runtime, leaving the old code running the way it was before weaving.

我在考虑AspectJ的加载时间编织+运行时类的加载/卸载会做到这一点.有人尝试过吗?有什么建议吗? 谢谢.

I'm thinking AspectJ load time weaving + runtime class loading/unloading would do it. Has anyone tried it? Any recommendations? Thank you.

推荐答案

要考虑的几件事:

  • 是的,您可以在类加载期间 进行LTW,但不能在类加载后 后进行LTW.
  • 没有类卸载的概念,因为要卸载的类需要进行垃圾回收,并且不再需要对该类的引用.即使是后者,JVM规范AFAIK仍将其声明为可选,无论是否卸载以及何时GC应当清除已经加载的类.您永远都不能依靠它.
  • True, you can do LTW during class loading, but not after a class has already been loaded.
  • There is no such concept as class unloading because for a class to be unloaded it needs to be garbage-collected and for that no references to the class must exist anymore. Even if the latter was the case, the JVM specification AFAIK declares it as optional whether or not and when unloading or GC should clear off an already loaded class. You could never rely on it.

话虽如此,您可以尝试诸如OSGi之类的概念,也可以编写自己的类加载器(或在Internet上找到许多现有的类加载器),以在单独的类加载器实例中加载每个类或每个JAR.这可能会变得非常复杂,因此,只要您处于您所处技术的范围之内,也许您就应该考虑使用这种简单方法:

Having said that, you either can try concepts such as OSGi or write your own class loader (or find one of many existing ones on the Internet) which loads each class or each JAR in a separate classloader instance. This can get arbitrarily complex, so maybe you want to consider this simple approach as long as it is within the technical bounds of your situation:

  • 将各方面编译为代码或使用LTW,这并不重要.只需确保在实际使用类之前就编写了方面代码.显然,编译时间还远远不够,加载时间仅够快,但还可以.
  • 使用 if()切入点建议,并提供一种方法来动态更改切入点使用的变量的值,以便能够动态打开和关闭建议.性能开销通常将是最小的,请放心.只需尝试一下,然后再说它太贵了.

此解决方案满足您的条件,可以动态(取消)激活它,并且在编织方面代码后无需重新启动JVM.

This solution satisfies your conditions that it can be (de)activated dynamically and that no JVM restart is necessary after the aspect code has been woven.

这篇关于运行时的方面编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆