Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Compare the fixes for self-invocation: extracting a bean, self-injection, AopContext.currentProxy() and AspectJ mode. Which would you choose in a code review?

Extracting the self-called method into a separate bean is the clearest fix, because the call then goes through a normal proxy and the design shows the boundary. Self-injection with @Lazy works but is easy to misuse and creates a circular reference; AopContext.currentProxy() needs exposeProxy = true and ties business code to Spring AOP; AspectJ mode weaves advice into bytecode so self-calls are advised, at the cost of build or agent setup; and TransactionTemplate makes the transaction boundary explicit in code. In a code review, I would ask for an extracted bean first, accept TransactionTemplate when the boundary belongs inside a method, and push back on the other two without a strong reason.

Why this question is asked

Every Spring team meets the self-invocation problem eventually: a method calls another method of the same bean, and the @Transactional, @Async or @Cacheable on the second method is silently ignored. There are five well-known ways around it, and they are not equally good. Interviewers ask this comparison because a senior developer is expected to know not only that the fixes exist but what each costs to the next person reading the code. In this answer, we will recap why self-calls skip the proxy, apply each fix to the same example, weigh them against each other, and finish with what we would accept in a code review.

1A Quick Recap of Self-Invocation

Spring applies annotations such as @Transactional through a proxy, an object generated around our bean. Other beans receive the proxy, so their calls pass through the advice. When a method inside the bean calls another method on this, the call goes straight to the real object, called the target, and the advice never runs. Every fix works by doing one of two things: making the call go through a proxy after all, or applying the behaviour without relying on the proxy.

Java
@Service
class InvoiceService {
public void closeMonth(List<Invoice> invoices) {
invoices.forEach(this::issue); // self-call: no transaction
}
@Transactional
public void issue(Invoice i) { ... }
}

2Fix 1: Extract a Separate Bean

We move issue() into its own bean, InvoiceIssuer, and inject it into InvoiceService. The call now leaves one bean and enters another through its proxy, which is exactly how the proxy model is meant to work.

Java
@Service
class InvoiceService {
private final InvoiceIssuer issuer;
InvoiceService(InvoiceIssuer issuer) { this.issuer = issuer; }
public void closeMonth(List<Invoice> invoices) { invoices.forEach(issuer::issue); } // tx per invoice
}
@Service
class InvoiceIssuer {
@Transactional public void issue(Invoice i) { ... }
}

Nothing about this is Spring-specific trickery. A reader sees a dependency and a transactional method, and the transaction works for every caller, including tests with a real context. The cost is one more class, which is usually a design improvement anyway, because the self-call often signals that the bean has two responsibilities.

3Fix 2: Self-Injection

Self-injection gives the bean a reference to its own proxy and calls through it.

Java
@Service
class InvoiceService {
@Lazy @Autowired private InvoiceService self; // the proxy, resolved lazily
public void closeMonth(List<Invoice> invoices) { invoices.forEach(self::issue); }
@Transactional public void issue(Invoice i) { ... }
}

It works, and it is one line. But the line is easy to misuse: a later developer may call issue() directly again, or copy the pattern without understanding it. It also creates a circular reference from the bean to itself. Spring Boot forbids circular references by default since 2.6, so the @Lazy, which injects a lazy-resolution proxy instead of the bean itself, is what makes it start. The code works for reasons that are invisible to most readers, which is the main objection.

4Fix 3: AopContext.currentProxy()

Spring can store the current proxy in a thread-local while an advised call runs, and AopContext.currentProxy() returns it. We must turn that on with exposeProxy = true.

Java
@EnableAspectJAutoProxy(exposeProxy = true)
@Configuration class AopConfig {}
@Service
class InvoiceService {
public void closeMonth(List<Invoice> invoices) {
InvoiceService proxy = (InvoiceService) AopContext.currentProxy(); // IllegalStateException if not exposed
invoices.forEach(proxy::issue); // through the proxy: tx per invoice
}
@Transactional public void issue(Invoice i) { ... }
}

This ties business code to Spring AOP's internals: the class now imports org.springframework.aop, casts to its own type, and fails at runtime with an IllegalStateException if the configuration flag is missing or if the method itself was not called through a proxy. It also only works when the outer call itself came in through the proxy on the same thread, so calling closeMonth() on the raw object, or from a thread the proxy never saw, fails. It is a tool for framework code, not for services.

5Fix 4: AspectJ Mode and Fix 5: TransactionTemplate

AspectJ mode, @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) with compile-time or load-time weaving, changes the class's bytecode so the transaction logic is inside issue() itself. Self-calls are then advised, and so are private methods. The code stays clean, but the build gains the AspectJ compiler or the application gains a weaving agent, every environment including tests must use it, and debugging woven code is less direct. It is a platform decision, not a fix for one class.

TransactionTemplate takes a different approach: it does not use the proxy at all. We write the transaction boundary in code, right where it belongs.

Java
@Service
class InvoiceService {
private final TransactionTemplate tx;
InvoiceService(PlatformTransactionManager tm) { this.tx = new TransactionTemplate(tm); }
public void closeMonth(List<Invoice> invoices) {
for (Invoice i : invoices) {
tx.executeWithoutResult(status -> issue(i)); // explicit: one transaction per invoice
}
}
private void issue(Invoice i) { ... } // plain method, no annotation needed
}
Comparing the five fixes
FixHow it worksMain costIn review
Extract a beancall crosses a bean boundary, goes through its proxyone more classpreferred
Self-injectioncalls its own proxy via a @Lazy fieldhidden trick, circular referencepush back
AopContext.currentProxy()fetches the proxy from a thread-localexposeProxy flag, couples code to Spring AOPpush back
AspectJ modeadvice woven into bytecodecompiler or agent in every environmentplatform decision only
TransactionTemplatetransaction written in code, no proxya few lines, transactions onlyaccept

The boundary is visible, it works on private methods and self-calls, and it lets us catch one invoice's failure and continue with the next. The cost is a few lines of plumbing, and it only applies to transactions; it does not help with @Async or @Cacheable.

6What I Would Choose in a Code Review

In a review, I would ask for the extracted bean first. It is the most ordinary Java, it keeps every proxy-based annotation working, and it usually improves the design. When the transaction boundary really belongs inside a method, such as one transaction per item in a loop with error handling per item, I would accept TransactionTemplate, because it makes the boundary explicit. I would push back on self-injection and AopContext.currentProxy() unless there is a constraint that rules out the others, and then I would ask for a comment explaining why. AspectJ mode is not something one pull request should introduce; it belongs to the platform team's decision for the whole service.

7Follow-up questions

Does self-injection work for @Async and @Cacheable too?

Yes, because it calls the real proxy, so all the proxy's advice runs. The same is true of AopContext.currentProxy(). TransactionTemplate only solves transactions; for caching, the equivalent explicit tool is to use the Cache API directly.

Is exposeProxy expensive?

It adds a thread-local set and reset around each advised call, which is cheap. The objection is not performance but coupling and fragility: the code depends on a configuration flag and on being called through a proxy.

Why does extracting a bean count as better design and not just a workaround?

Because the self-call usually means one bean does two things, such as orchestrating a batch and processing one item. Splitting them gives each a clear responsibility, makes the transaction boundary part of the public contract of the item processor, and makes each easier to test.

8Conclusion

In this answer, we compared five fixes for self-invocation. Extracting a separate bean is the clearest and keeps proxies working normally. Self-injection with @Lazy works but hides a trick and a circular reference, and AopContext.currentProxy() needs exposeProxy = true and couples code to Spring AOP. AspectJ mode advises self-calls by weaving bytecode, at the cost of build or agent setup everywhere. TransactionTemplate makes transaction boundaries explicit in code. In review we prefer the extracted bean, accept TransactionTemplate, and push back on the rest without a strong reason.

What to say in the interviewAbout 90 seconds aloud

Open with

All five fixes either make the call go through a proxy or apply the behaviour without one. Extracting the method into another bean is my first choice: the call crosses a bean boundary, goes through that bean's proxy, every annotation works, and the self-call usually meant the class had two responsibilities anyway.

Then, if they want more

Self-injection, a @Lazy autowired field of the bean's own type, also calls the proxy, but it is a trick the next reader may not understand, it is a circular reference that Spring Boot forbids by default without the @Lazy, and it is easy to bypass by accident.

AopContext.currentProxy needs exposeProxy set to true, throws if the flag is missing or the outer call was not advised, and couples business code to Spring AOP, so I see it as framework-level code.

AspectJ mode solves the problem properly by weaving the advice into bytecode, so self-calls and even private methods work, but it brings the AspectJ compiler or a weaving agent into every build and environment, so it is a platform decision rather than a fix for one class.

TransactionTemplate avoids proxies entirely and makes the transaction boundary explicit, which is great for per-item transactions in a loop, but it only covers transactions. So in a review I would ask for the extracted bean, accept TransactionTemplate where the boundary belongs inside the method, and question the other two.

About this answer

Last updated
12 November 2026
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 "Compare the fixes for self-invocation: extracting a bean, self-injection...": https://scrivio-interview-library.netlify.app/spring-boot/aop/5ea59962fe/