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.
Load-time weaving (LTW) is simply binary weaving defered until the point that a class loader loads a class file and defines the class to the JVM. To support this, one or more "weaving class loaders", either provided explicitly by the run-time environment or enabled through a "weaving agent" are required.
If you have grasped the basic concepts of AOP (moreover AspectJ) and want to know the details about different types of AspectJ weaving, which are Compile-time weaving, Post-compile (binary) weaving, and Load-time weaving, do continue reading this article.
Weaving in AspectJ Classes are defined using Java syntax. The weaving process consists of executing the aspect advice to produce only a set of generated classes that have the aspect implementation code woven into it.
AspectJ is very mature, powerful and widely used in today's enterprise Java frameworks like Spring. Spring uses AspectJ to make it easy for the developers to use Transaction Management, or applying security (using Spring Security) in your application.
A few things to consider:
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:
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.
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:
(Source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With