What is Spring Framework?
A set of modules for building Java apps (DI/IoC, MVC, AOP, data access, transactions, security). It provides the container that creates and wires objects (beans).
What is Spring Boot?
Spring Boot is an opinionated layer on top of Spring that provides auto-configuration, starter dependencies, embedded server setup, and production-ready features (e.g., Actuator) to bootstrap apps quickly.
Spring vs Spring Boot (key difference)
Spring = core framework and libraries; Boot = auto-configuration + conventions + starters + easier app bootstrapping and packaging.
What is IoC (Inversion of Control)?
A design principle where the container/framework controls object creation and wiring instead of your code creating dependencies directly.
What is Dependency Injection (DI)?
A pattern where dependencies are provided to a class (via constructor/setter/field) instead of the class instantiating dependencies itself.
Why is DI useful?
Testability (easy mocking), loose coupling, easier refactoring, centralized lifecycle management, swapping implementations without changing consumers.
What is a Spring ApplicationContext?
The IoC container that holds bean definitions, creates beans, injects dependencies, and manages bean lifecycle.
What is a Bean in Spring?
An object managed by the Spring container (created, wired, and lifecycle-managed by Spring).
How are beans created in Spring?
By component scanning annotations (@Component, @Service, etc.) or by @Bean methods inside @Configuration classes.
What does @Component do?
Marks a class for component scanning so Spring can detect it and register it as a bean.
What is the purpose of @Service?
Semantically marks business/service layer beans. Functionally similar to @Component (but helps readability and tooling).
What is the purpose of @Repository?
Marks persistence layer. Also enables exception translation (converts persistence exceptions into Spring’s DataAccessException hierarchy).
What is the purpose of @Controller?
Marks a web controller (MVC). Typically returns views (not necessarily JSON).
What is @RestController?
Combination of @Controller + @ResponseBody. Methods return objects serialized as JSON/XML by default (typically JSON).
What is @Configuration?
Indicates a class contains bean definitions. Spring may proxy it to ensure @Bean methods return singletons consistently.
What is @Bean?
Defines a bean via a factory method (often used for third-party classes you can’t annotate).
What is the default bean scope in Spring?
Singleton (one instance per application context).
What does singleton scope mean?
One shared bean instance per Spring context. Must be thread-safe if used in web apps (multiple requests concurrently).
What is prototype scope?
A new bean instance is created each time it’s requested/injected.
What are request/session scopes?
Web scopes. Request = one bean per HTTP request; Session = one bean per HTTP session (web apps).
Why prefer constructor injection?
Makes dependencies explicit, supports immutability (final), prevents partially-initialized objects, easier unit testing.
Why avoid field injection?
Hides dependencies, harder to test, harder to construct without Spring, can complicate immutability and reflection-based issues.
What is component scanning?
Spring scans packages for stereotype annotations (@Component, @Service, etc.) and automatically registers them as beans.
How does Spring resolve which bean to inject when multiple candidates exist?
Using @Primary, @Qualifier, bean name, or by type+name matching.