Aspect weaving at runtime

后端 未结 3 763
旧时难觅i
旧时难觅i 2021-02-15 12:21

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

相关标签:
3条回答
  • 2021-02-15 13:07

    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.

    0 讨论(0)
  • 2021-02-15 13:12

    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.
    0 讨论(0)
  • 2021-02-15 13:20

    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.

    enter image description here
    enter image description here

    (Source)

    0 讨论(0)
提交回复
热议问题