About Us Contact Us Write for Us Advertise
Home > Java > Spring Boot Interview Questions (Auto-config, Proxies & @Transactional) — Freshers to Senior
Java

Spring Boot Interview Questions (Auto-config, Proxies & @Transactional) — Freshers to Senior

A senior-level Spring Boot interview guide covering IoC/DI, constructor injection, bean lifecycle and scopes, auto-configuration internals with @Conditional, JDK vs CGLIB proxies, the @Transactional self-invocation trap, transaction propagation/rollback, N+1 queries, and configuration/profiles.

Shiv Pandey
Shiv Pandey
Oct 10, 2026 | 165 views
Spring Boot Interview Questions (Auto-config, Proxies & @Transactional) — Freshers to Senior

Spring Boot interviews reward people who understand the magic — not just the annotations, but what happens under them. Anyone can put @Transactional on a method; the senior question is "why does calling it from another method in the same class do nothing?" This is a deep Spring Boot interview guide: the IoC container and bean lifecycle, how auto-configuration actually decides what to wire, proxy-based AOP and the @Transactional self-invocation trap, transaction propagation and rollback rules, the N+1 query problem, and the design reasons behind constructor injection.

The theme interviewers probe: Spring is "convention over configuration" powered by proxies and conditional beans. Almost every hard question — transactions, caching, self-invocation, why a bean did/didn't load — traces back to those two mechanisms. Understand them and the rest is detail.

IoC, DI & the container

Q1. What is Inversion of Control and Dependency Injection?

IoC inverts who controls object creation: instead of your code doing new UserService(), the Spring container creates, wires, and manages objects (beans). DI is how it supplies collaborators — via constructor, setter, or field. The payoff is loose coupling and testability (you inject a mock). This is the Dependency Inversion principle from SOLID made concrete.

Q2. Constructor vs field vs setter injection — which and why?

Constructor injection is the recommended default: dependencies are final (immutable), the object is never in a half-built state, mandatory dependencies are explicit, and it's trivial to test without Spring (just call new). Field injection (@Autowired on a field) hides dependencies, can't be final, and needs reflection to test — avoid it. Setter injection suits genuinely optional dependencies.

Q3. Explain the bean lifecycle.

// instantiate -> populate deps -> Aware callbacks
// -> BeanPostProcessor.before -> @PostConstruct / afterPropertiesSet
// -> BeanPostProcessor.after (proxies created HERE) -> READY
// ... on shutdown: @PreDestroy / destroy()

The key insight: proxies (for @Transactional, @Async, @Cacheable) are created by a BeanPostProcessor after initialisation. That's why the container hands other beans a proxy, not your raw object — central to Q7.

Q4. Bean scopes — and the singleton-holding-prototype trap.

Default is singleton (one instance per container). Others: prototype (new instance each injection), and web scopes request/session/application. Trap: inject a prototype bean into a singleton and you get one prototype instance frozen at singleton-creation time — not a fresh one per call. Fix with ObjectProvider, a lookup method, or scoped-proxy.

Q5. Resolving ambiguity — @Primary vs @Qualifier.

When two beans satisfy one type, Spring can't choose. @Primary marks a default winner; @Qualifier("name") picks a specific one at the injection point (more explicit, wins over @Primary). Without either you get NoUniqueBeanDefinitionException.

Auto-configuration — the Boot magic

Q6. How does Spring Boot auto-configuration actually work?

@SpringBootApplication bundles @Configuration + @EnableAutoConfiguration + @ComponentScan. At startup, Boot reads auto-configuration classes listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (the old spring.factories), then each class is gated by @Conditional annotations:

@ConditionalOnClass(DataSource.class)       // only if the class is on classpath
@ConditionalOnMissingBean              // back off if you defined your own
@ConditionalOnProperty("feature.enabled")  // only if a property is set

So a starter puts jars on the classpath; the conditions detect them and wire sensible defaults — and @ConditionalOnMissingBean means your bean always overrides Boot's. That "define it and Boot backs off" behaviour is the whole model. Debug what loaded with the Actuator conditions endpoint or --debug.

Q7. What is a starter?

A curated dependency bundle (e.g. spring-boot-starter-web pulls Spring MVC + Jackson + embedded Tomcat) with compatible versions, so you add one dependency instead of a dozen. It exists to trigger the right auto-configurations.

Proxy-based AOP & @Transactional (the killer round)

Q8. Explain the @Transactional self-invocation trap.

class OrderService {
    public void place() {
        save();  // internal call — bypasses the proxy!
    }
    @Transactional
    public void save() { /* NO transaction actually starts */ }
}

Why: @Transactional works via a proxy that wraps your bean. Calls from outside go through the proxy (transaction starts); a call from place() to save() is a plain this.save() that never touches the proxy — so no transaction. Same trap hits @Async and @Cacheable. Fixes: move the annotated method to another bean, self-inject the proxy, or use AopContext.currentProxy(). This is the classic senior Spring question.

Q9. JDK dynamic proxy vs CGLIB — which does Spring use?

If the bean implements an interface, Spring uses a JDK dynamic proxy (proxies the interface). Otherwise it uses CGLIB (subclasses the class at runtime) — which is why final classes/methods can't be proxied. Spring Boot defaults to CGLIB even for interfaces since 2.0.

Q10. Transaction propagation & rollback rules.

PropagationBehaviour
REQUIRED (default)Join existing tx, or start one
REQUIRES_NEWSuspend current, always start a new tx
NESTEDSavepoint inside the current tx
SUPPORTSUse tx if present, else non-tx

Rollback gotcha: by default Spring rolls back only on unchecked (RuntimeException) and Error — a thrown checked exception commits! Override with @Transactional(rollbackFor = Exception.class). Interviewers ask this constantly.

Web layer & data

Q11. @Controller vs @RestController; global exception handling?

@RestController = @Controller + @ResponseBody — every method returns data (JSON) rather than a view name. For consistent error responses across controllers, use a @RestControllerAdvice class with @ExceptionHandler methods — one place to map exceptions to HTTP status + body.

Q12. What is the N+1 query problem and how do you fix it?

// 1 query for orders, then 1 lazy query PER order for its items = N+1
for (Order o : orderRepo.findAll())   // 1 query
    o.getItems().size();               // N more queries
// FIX: fetch join
@Query("select o from Order o join fetch o.items")

Lazy associations trigger a query on each access. Fix with a JOIN FETCH, an @EntityGraph, or batch fetching. This is the most common JPA performance question — know it cold.

Q13. LAZY vs EAGER fetching — default and advice.

@OneToMany/@ManyToMany default to LAZY; @ManyToOne/@OneToOne default to EAGER. Advice: keep everything LAZY and fetch explicitly when needed — EAGER causes surprise joins and the N+1 issues. Accessing a lazy field outside a transaction throws LazyInitializationException.

Config & profiles

Q14. @Value vs @ConfigurationProperties?

@Value("${x}") injects a single property. @ConfigurationProperties(prefix="app") binds a whole group of properties to a typed POJO — type-safe, supports validation and nesting, and is the preferred approach for anything beyond one value. Use @Profile and application-{profile}.yml to vary config per environment.

Rapid-fire

  • Spring vs Spring Boot? Boot adds auto-config, starters, embedded server, and opinionated defaults on top of the Spring framework. See Spring vs Spring Boot.
  • Filter vs Interceptor? Filter is servlet-level (before DispatcherServlet); Interceptor is Spring MVC-level (has access to handler/model).
  • How does Boot embed Tomcat? The web starter includes it; Boot boots an embedded servlet container so you run a plain java -jar.
  • Circular dependency? Two beans needing each other — constructor injection fails fast; Spring can resolve setter/field cycles but it's a design smell.
  • @SpringBootApplication scans where? The package of the main class and below — put it at the root.
  • Actuator? Production endpoints (/health, /metrics, /conditions) for monitoring.

Go deeper by topic

Here's what carried me through Spring Boot rounds: I stopped memorising annotations and started asking "what is Spring secretly doing when it sees this?" The answer is almost always one of two things — it's registering a conditional bean, or it's wrapping my object in a proxy. Once that clicked, the hard questions answered themselves: self-invocation fails because the proxy isn't involved; my custom bean wins because auto-config backs off on @ConditionalOnMissingBean; a checked exception commits because the proxy's rollback rule only triggers on unchecked. So when you prep, don't collect annotations — trace the mechanism behind three or four of them, and you'll reconstruct the rest live. That "I understand the machinery" signal is exactly what turns a Spring interview into an offer. You've got this.

Related

← Exception interview questions · DSA interview questions →

Related Articles

DSA Interview Questions in Java (Patterns, Not Problems) — Freshers to Senior
Java

DSA Interview Questions in Java (Patterns, Not Problems) — Freshers to Senior

Java Exception Handling Interview Questions (Traps & Best Practices) — Freshers to Senior
Java

Java Exception Handling Interview Questions (Traps & Best Practices) — Freshers to Senior

Java Multithreading Interview Questions (JMM, Locks & Traps) — Freshers to Senior
Java

Java Multithreading Interview Questions (JMM, Locks & Traps) — Freshers to Senior

Java Collections Interview Questions (Internals & Traps) — Freshers to Senior
Java

Java Collections Interview Questions (Internals & Traps) — Freshers to Senior