I've seen this term when read about how Spring works and I've just read the article about JPA implementation performance and it has the next statistics:
EclipseLink 3215 ms (Run-time weaver - Spring ReflectiveLoadTimeWeaver weaver ) EclipseLink (Build-time weaving) 3571 ms EclipseLink (No weaving) 3996 ms
So, could someone explain in plain English, what is weaving?
Thanks!
8 Answers
Weaving is generating or editing code by directly modifying existing .class (byte-code) files. This can occur at different points in the application life cycle.
- Outside of JVM at compile time at packaging time
- Inside a JVM at class load time. after a class has been loaded.
Spring Framework uses this for AOP functionality. Eclipselink uses weaving for lazy loading or change tracking.
From here:
In Spring AOP makes it possible to modularize and separate logging, transaction like services and apply them declaratively to the components Hence programmer can focus on specific concerns. Aspects are wired into objects in the spring XML file in the way as JavaBean. This process is known as 'Weaving'.
In nutshell, we could say
Weaving is the process of applying the Advices to the Target objects at given pointcuts to get the Proxy Objects.
Weaving is the process of linking aspects with other application types or objects to create an advised object. Weaving can be done at compile time, at load time, or at runtime.
There are two ways in which classes and aspects can be woven: static or dynamic.
Spring AOP does dynamic weaving of aspects by creating proxy of the target objects.
It uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice).
1I found this description useful:
Weaving: This is the process of inserting aspects into the application code at the appropriate point. For compile-time AOP solutions, this weaving is generally done at build time. Likewise, for runtime AOP solutions, the weaving process is executed dynamically at runtime [using JDK dynamic proxy and CGLIB proxy]. AspectJ supports another weaving mechanism called load- time weaving (LTW), in which it intercepts the underlying JVM class loader and provides weaving to the bytecode when it is being loaded by the class loader.
reference: Pro Spring 5: An In-Depth Guide to the Spring Framework and Its Tools
Object-oriented software systems that are developed using aspect-oriented programming techniques consist of classes and aspects. Classes implement the primary functionality of an application, for example, managing stocks or calculating insurance rates. Aspects, on the other hand, capture technical concerns like persistence, failure handling, communication, or process synchronization.
There are two ways in which classes and aspects can be woven: static or dynamic.
Static weaving means to modify the source code of a class by inserting aspect-specic statements at join points.In other words: aspect code is inlined into classes. The result is highly optimized woven code, whose execution speed is comparable to that of code written without using aspects.
Weaving is a technique of manipulating the byte-code of compiled Java classes.
Cheers!
Weaving is the process of linking aspect with other application types or objects to create an advised object. Weaving can be done at compile time, load time or runtime. Spring AOP performs weaving at runtime.