Boost Java Performance with Cglib BeanCopier: Fast Bean Copy Techniques
This article introduces the high‑performance Cglib BeanCopier used in the Mica microservice framework, compares its speed with other bean‑copy tools, demonstrates usage with Spring and Lombok, explains the underlying ASM bytecode generation, and discusses current limitations such as lack of chainable beans and primitive‑wrapper handling.
Column Introduction
This series introduces a set of enhancements to Cglib bean copy in the Mica microservice core framework, aiming to ensure high performance while improving usability. The series contains six articles; interested readers are encouraged to follow.
Column Directory
cglib bean copy introduction.
mica bean copy introduction and support for chained bean copy.
mica bean supports copying primitive and wrapper types.
mica bean supports copying map to bean.
Enhance mica bean copy with Spring type conversion.
mica bean and Map conversion enhancements and summary.
Cglib BeanCopier Introduction
Alibaba's p3c plugin recommends avoiding Apache BeanUtils for property copy due to poor performance, suggesting alternatives such as Spring BeanUtils or Cglib BeanCopier. The focus here is on Cglib's BeanCopier.
Performance
The following chart from GitHub compares the performance of various bean copy tools, showing a significant performance gap.
The chart demonstrates that Cglib BeanCopier is extremely fast, which explains its recommendation in Alibaba's guidelines. Below is its usage example.
Usage
Cglib is included in Spring core source; users of Spring or Spring Boot can use it directly. Others need to add the dependency. The following examples assume Spring usage and require Lombok.
User Object
<code>@Data
public class User {
private Integer id;
private String name;
private Integer age;
}</code>UserVo Object
<code>@Data
public class UserVo {
private String name;
private Integer age;
}</code> <code>import org.springframework.cglib.beans.BeanCopier;
public class UserCopyTest {
public static void main(String[] args) {
// 1. Initialize user and set values
User user = new User();
user.setId(250);
user.setName("如梦技术");
user.setAge(30);
// 2. Initialize userVo
UserVo userVo = new UserVo();
// 3. Create BeanCopier (no type conversion)
BeanCopier copier = BeanCopier.create(User.class, UserVo.class, false);
// 4. Copy properties (no converter needed)
copier.copy(user, userVo, null);
// 5. Print result: UserVo(name=如梦技术, age=30)
System.out.println(userVo);
}
}</code>Principle
Cglib BeanCopier achieves high performance by leveraging ASM bytecode generation. The generated code is shown below.
Set the Cglib source generation directory (recommended to place it in the IDEA project to view the generated class bytecode):
<code>// Set Cglib source generation directory
String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);</code>Running the main method prints a log line indicating the debug location:
<code>CGLIB debugging enabled, writing to '/Users/lcm/git/mica/mica-example/web-example/src/test/java'</code>The following image shows the generated code, illustrating that Cglib BeanCopier creates the necessary getter and setter conversions.
Cglib Copy Issues
Does not support chained beans.
Does not support copying between primitive and wrapper types (
int <-> Integer).
Type conversion is not intelligent; enabling useConverter and overriding Converter causes even identical types to go through conversion logic.
Note: Detailed discussion of these issues will be provided in future articles. Please follow and subscribe for updates.
Links
Mica: https://github.com/lets-mica/mica
Dreamlu Technology website: https://www.dreamlu.net
References
[1]https://github.com/lets-mica/mica
[2]https://www.dreamlu.net
Open Source Recommendations
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.