Spring Interview Questions
Commonly asked core Spring Framework interview questions with clear, practical answers.
A curated set of core Spring Framework interview questions — the conceptual foundation interviewers expect before moving on to Spring Boot, Spring MVC, or Spring Data specifics.
Q: What is Inversion of Control, and how does Dependency Injection relate to it?
Inversion of Control is the principle that an object should not be responsible for constructing or locating its own dependencies — control over object creation is "inverted" from the object itself to an external container. Dependency Injection is the specific mechanism Spring uses to achieve IoC: the container constructs each bean's dependencies and passes ("injects") them in, typically through the constructor, rather than the class calling new or a service locator itself.
Q: Why is constructor injection preferred over field injection?
Constructor injection allows dependencies to be declared final, guaranteeing the object is never in a partially-initialized state and can't be reassigned after construction. It also makes the class trivially testable with plain new SomeService(mockDependency) calls, with no reflection or Spring context required, and it makes circular dependencies fail loudly at startup instead of silently working around them. Field injection (@Autowired on a private field) compiles and runs fine but gives up all three of these benefits.
Q: What's the difference between @Component, @Service, and @Repository?
All three are stereotype annotations that mark a class as a Spring-managed bean, and functionally @Service and @Repository behave like @Component for the purposes of component scanning and dependency injection. The distinction is semantic: @Service signals business/domain logic and @Repository signals a data-access class — and @Repository additionally enables Spring's exception translation, converting persistence-technology-specific exceptions (e.g. a JDBC SQLException) into Spring's unified DataAccessException hierarchy.
Q: What's the difference between @Component and a @Bean method?
@Component (and its specializations) is placed directly on a class you own, and Spring discovers and registers it automatically via component scanning. @Bean is placed on a method inside an @Configuration class and is used when you need to construct a bean explicitly — most commonly for third-party classes you don't own and can't annotate, or when the construction logic itself needs code (builder calls, conditional setup) rather than just a bare constructor.
Q: What are Spring bean scopes, and when would you use something other than the default?
The default scope is singleton — one shared instance per container, appropriate for the large majority of beans since services and repositories are typically stateless. prototype scope creates a new instance every time the bean is requested, and is appropriate for a bean that accumulates mutable, non-thread-safe state across a single use. Web-aware scopes like request and session scope a bean's lifetime to a single HTTP request or session, respectively, and are used for holding per-request or per-user state safely.
Q: How do Spring profiles help manage different environments?
A profile is a named configuration group that's conditionally activated (via spring.profiles.active), letting you register different beans (@Profile("dev") vs @Profile("prod")) or load different property files (application-dev.yml vs application-prod.yml) for different environments without branching logic in application code. This keeps environment-specific concerns — database URLs, external service credentials, feature toggles — out of the codebase and lets the exact same build be promoted from dev to staging to production by changing only which profile is active.
Q: What problem does AOP solve, and what's the difference between an aspect, advice, and a pointcut?
AOP addresses cross-cutting concerns — logging, timing, transactions, security — that would otherwise need to be duplicated inside every method they apply to. An aspect is the module containing that logic (a class annotated @Aspect); advice is the actual code that runs at a matched point (@Before, @Around, etc.); a pointcut is the expression that selects which methods the advice applies to. Spring's own @Transactional and @Cacheable are themselves implemented using this exact mechanism.
Q: How does Spring AOP actually intercept method calls, and why doesn't advice run on a self-invoked call?
Spring AOP is proxy-based: for any bean matched by an aspect's pointcut, Spring registers a proxy in the container instead of the original object, and every call into that bean goes through the proxy first, which runs the relevant advice before/after/around the real call. Spring picks a JDK dynamic proxy when the bean implements an interface, or a CGLIB subclass proxy otherwise. Because advice only fires on calls that pass through the proxy, a method calling another method on this directly bypasses the proxy entirely and its advice never runs — the well-known "self-invocation" limitation, usually fixed by moving the advised method to a different bean invoked through it, or by injecting a @Lazy self-reference to obtain the bean's own proxy.
Q: What's the difference between testing with @SpringBootTest and a plain unit test that constructs its dependencies by hand?
A plain unit test (new OrderService(fakeRepository)) involves no Spring at all — it's fast and appropriate for testing one class's logic in isolation. @SpringBootTest starts a real ApplicationContext, wiring every bean the way the actual application would at runtime, which is slower but verifies the beans are actually wired correctly together. @MockBean (or its newer replacement, @MockitoBean, since Spring Boot 3.4) lets a @SpringBootTest swap out one specific dependency for a Mockito mock while keeping the rest of the real wiring intact — useful for isolating an external call (like a payment gateway) without giving up the realism of testing the actual managed bean.