Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

With a CGLIB proxy, a bean's constructor logs a line. In older Spring versions it logged twice. Why, and what changed?

A CGLIB proxy is an instance of a generated subclass of our bean, and in older Spring versions it was created with a normal constructor call, which runs the superclass constructor, our bean's constructor, a second time. Since Spring Framework 4.0, Spring creates CGLIB proxy instances with Objenesis, which allocates the object without calling any constructor, so the constructor runs once, for the real target only. The proxy instance's own fields therefore stay uninitialised, which is harmless because advised calls go to the target, but direct field access on the proxy, or a final method running on the proxy, sees those empty fields.

Why this question is asked

This question looks like history, but the answer explains a real class of bugs that still appear today: a NullPointerException on a field that is definitely injected, or a value that is set on the bean but reads as null through the proxy. Interviewers ask it to see whether we understand that a proxy is a separate object with its own fields, not just a pointer to our bean. In this answer, we will recap what a CGLIB proxy is, see why the constructor used to run twice, look at how Objenesis changed that, and then see the consequences of a proxy whose fields are never set.

1A Quick Recap: The Proxy Is a Subclass Instance

When Spring needs to add behaviour such as transactions to a bean and uses CGLIB, it generates a subclass of the bean's class at runtime, for example InvoiceService$$SpringCGLIB$$0. The subclass overrides each non-final method so that it runs the advice and then calls the real bean, which Spring calls the target.

So at runtime there are two objects. The target is our InvoiceService, created normally, with its dependencies injected. The proxy is an instance of the generated subclass, which Spring injects into other beans. It holds a reference to the target through its callbacks. Because it is a subclass, it also inherits every field that InvoiceService declares, and those inherited fields are separate from the target's fields.

2Why the Constructor Used to Run Twice

In Java, creating an instance of a subclass with new always runs a constructor of the superclass first. So when early Spring versions created the proxy instance, the JVM ran InvoiceService's constructor for the proxy, after it had already run for the target. Any logging, counting or side effect in the constructor happened twice. Worse, CGLIB needed a usable constructor: classes with only constructors that take arguments were awkward to proxy, which pushed people toward a no-argument constructor just for the proxy's sake.

Java
@Service
public class InvoiceService {
private final InvoiceRepository repo;
public InvoiceService(InvoiceRepository repo) {
this.repo = repo;
log.info("InvoiceService created");
}
@Transactional
public void issue(Invoice i) { repo.save(i); }
}
// Before Spring 4.0 with a CGLIB proxy (when CGLIB found a constructor it could call):
// InvoiceService created (the target)
// InvoiceService created (the proxy instance, constructor run again)
// Spring 4.0 and later:
// InvoiceService created (the target only)

Notice that the second log line came from an object nobody uses for its state. Its constructor had run only because Java requires it for a normal instantiation.

3What Changed: Objenesis

Spring Framework 4.0 introduced ObjenesisCglibAopProxy. Objenesis is a small library, repackaged inside spring-core, that can create an instance of a class without calling any of its constructors. On HotSpot it uses the JVM's internal support for allocating an object directly, the same kind of mechanism that Java serialisation uses. Spring generates the CGLIB subclass, asks Objenesis for an instance of it, and then attaches the callbacks that forward calls to the target.

The result is that the bean's constructor runs exactly once, for the target, and classes with only argument constructors, which is the normal case with constructor injection, can be proxied without any special help. If Objenesis cannot be used on some unusual JVM, Spring falls back to calling a constructor, and the spring.objenesis.ignore property can force that fallback, so check your environment if you see the old behaviour.

What each object holds after startup
ObjectConstructor ran?repo fieldUsed for
Target InvoiceServiceYes, onceinjected repositoryruns the real method bodies
CGLIB proxy instanceNo (Objenesis)nullruns advice, forwards calls to the target
Two objects, two sets of fields; only the target's are initialised.

4The Consequence: The Proxy's Own Fields Are Empty

Since no constructor ran for the proxy instance, its inherited fields keep their default values: null for references, 0 for numbers, false for booleans. Normally nobody notices, because every overridden method forwards to the target, and the target's fields are properly set. The empty fields only show up when code touches the proxy's state directly instead of calling an advised method.

The first way is a final method. CGLIB cannot override it, so calling it on the proxy runs the inherited method body on the proxy instance itself, and that body reads the proxy's null fields.

Java
@Service
public class InvoiceService {
private final InvoiceRepository repo;
public InvoiceService(InvoiceRepository repo) { this.repo = repo; }
@Transactional public void issue(Invoice i) { repo.save(i); } // forwarded: repo is set
public final long count() { return repo.count(); } // runs on the proxy: repo is null
}
invoiceService.issue(inv); // works
invoiceService.count(); // NullPointerException: this.repo is null
Step throughForwarded call versus final method
CallerProxy instanceTarget
issue(inv)
advice, then issue(inv)
saved with target.repo
count() is final
runs inherited body on the proxy
NullPointerException: proxy.repo is null

6 messages in order. Play them, or step through them one at a time.

Only overridden methods reach the target; a final method stays on the empty proxy.

The second way is direct field access from outside the bean, for example a non-private field read by another class in the same package, or a test that reads a field through reflection. Such code reads the proxy's copy of the field, not the target's, so it sees null even though the target has a value. The cure in both cases is the same: keep fields private, never make proxied methods final, and only interact with a bean through its non-final methods.

5Practical Takeaways

Constructor side effects are now safe from duplication, but it is still wise to keep constructors simple and put startup work in @PostConstruct or an ApplicationRunner, which run only on the target. Fields of beans should be private and, with constructor injection, final, and proxied methods should never be final. When a test needs the real object behind a proxy, AopTestUtils.getTargetObject(bean) returns it, which is far safer than reading fields on the proxy.

6Follow-up questions

Does the same issue exist with JDK dynamic proxies?

No. A JDK proxy implements the bean's interfaces and is not a subclass of the bean, so it has no copies of the bean's fields and never runs the bean's constructor. Interfaces cannot declare instance fields, so direct field access through the proxy type is not possible.

Why does a final field still show null on the proxy?

Because final fields are assigned in the constructor, and no constructor ran for the proxy instance. Objenesis allocates the object with every field at its default value, so even a final reference field is null on the proxy.

Can the bean tell whether it is running as the target or the proxy?

Inside a normal advised method, this is always the target, because the proxy forwards the call. Only code that runs on the proxy instance itself, such as a final method, has this equal to the proxy.

7Conclusion

In this answer, we saw that a CGLIB proxy is a separate instance of a generated subclass of our bean. Older Spring versions created it with a constructor call, so the bean's constructor ran twice. Since Spring Framework 4.0, Spring uses Objenesis to allocate proxy instances without calling constructors, so the constructor runs once, for the target. The proxy's own fields therefore stay null, which is harmless for forwarded calls but breaks final methods and direct field access on the proxy.

What to say in the interviewAbout 80 seconds aloud

Open with

A CGLIB proxy is an instance of a generated subclass of the bean, and in Java instantiating a subclass normally runs the superclass constructor. In older Spring versions the proxy instance was created that way, so the bean's constructor ran once for the real target and again for the proxy, and you saw the log line twice.

Then, if they want more

It also forced people to provide no-argument constructors. Since Spring Framework 4.0, Spring uses ObjenesisCglibAopProxy: Objenesis creates the instance of the proxy class without calling any constructor, and Spring then attaches the callbacks that forward to the target. So the constructor runs once, only for the target.

The side effect is that the proxy instance's inherited fields are never initialised, they are null or zero, even final ones. That is harmless because every overridden method forwards to the target, whose fields are set.

It only bites when code touches the proxy's own state: a final method, which CGLIB cannot override, runs on the proxy and can throw a NullPointerException on an injected field, and direct field access from outside reads the proxy's empty copy instead of the target's.

So I keep fields private, never make proxied methods final, and in tests use AopTestUtils.getTargetObject if I need the real object.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "With a CGLIB proxy, a bean's constructor logs a line. In older Spring versions it logged...": https://scrivio-interview-library.netlify.app/spring-boot/aop/6dc2cc67ec/