Skip to main content
InterviewHubPRO
Section Overview
Q6

Question 6 of 50

Part 1: Spring Core, Auto-Configuration & Architecture (1 - 10)

12%
Spring Boot & Microservices (50 Qs)•Part 1: Spring Core, Auto-Configuration & Architecture (1 - 10)

How does Spring AOP work, and how does `@Transactional` leverage dynamic proxies?

senior LevelMost AskedSystem DesignDatabases & SQL

In Simple Words: Spring AOP (Aspect-Oriented Programming) intercepts method execution using Dynamic Proxies (CGLIB class-based or JDK dynamic interface-based). When a class has @Transactional:

  1. Spring generates a proxy wrapper around the target bean.
  2. The caller invokes the proxy's method.
  3. The proxy opens a database transaction (connection.setAutoCommit(false)).
  4. The proxy executes your method inside a try-catch block.
  5. If successful, it commits (connection.commit()). If a RuntimeException occurs, it rolls back (connection.rollback()).
  6. Gotcha: Calling an @Transactional method internally from within the same class bypasses the proxy, meaning transaction management will NOT run.