Aspect weaving at runtime

依然范特西╮ 提交于 2020-01-31 09:38:33

问题


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.

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


回答1:


A few things to consider:

  • 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.

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:

  • Compile your aspects into your code or use LTW, it does not really matter. Just make sure that the aspect code is woven before the classes are actually used. Compile time is obviously more than soon enough, load time is just barely soon enough, but fine.
  • Use if() pointcuts for all relevant advice and provide a means to dynamically change the value of the variable used by the pointcuts so as to be enable to dynamically switch advice on and off. The performance overhead will usually be minimal, don't worry. Just give it a try before you say it is too expensive.

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.




回答2:


Aspect

An aspect is the cross-cutting functionality you are implementing. It is the aspect, or area, of your application you are modularizing. The most common (albeit simple) example of an aspect is logging. Logging is something that is required throughout an application. However, because applications tend to be broken down into layers based on functionality, reusing a logging module through inheritance

does not make sense. However, you can create a logging aspect and apply it throughout your application using AOP.


Weaving

Weaving is the process of applying aspects to a target object to create a new, proxied object. The aspects are woven into the target object at the specified joinpoints. The weaving can take place at several points in the target class’s lifetime:

  • Compile time :Aspects are woven in when the target class is compiled. This requires a special compiler.
  • Classload time :Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class’s bytecode before the class is introduced into the application.
  • Runtime :Aspects are woven in sometime during the execution of the application. Typically, an AOP container will dynamically generate a proxy class that will delegate to the target class while weaving in the aspects.


(Source)




回答3:


You can Implement an ASM solution that would allow to weave new code on top of already running code at runtime (with no downtime)

  • ASM can retransform already loaded classes it basically reads the class on the file and retransform the loaded class in Memory
  • Manipulation with ASM (https://www.baeldung.com/java-asm)

You might can use the same solution to remove the woven at runtime

  • the loaded class remains on the file system that might can be reload.


来源:https://stackoverflow.com/questions/17323500/aspect-weaving-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!