Core Spring: IoC & dependency injection
What is dependency injection and inversion of control?
Inversion of control means the framework, not your code, creates and wires objects. Dependency injection is how it's delivered: instead of a class constructing its collaborators, they're supplied to it (via constructor, setter, or field). This decouples classes and makes them easy to test with mocks.
What is a Spring Bean and the application context?
A bean is any object the Spring IoC container manages. The application context is that container - it instantiates beans, injects their dependencies, and manages their lifecycle. You declare beans with stereotype annotations (@Component, @Service, @Repository, @Controller) or @Bean methods in a @Configuration class.
Constructor injection vs field injection - which is preferred?
Constructor injection is preferred: it makes dependencies explicit and mandatory, allows the field to be final (immutable), and works without Spring in unit tests. Field injection (@Autowired on a field) is concise but hides dependencies and can't be made final.
What are common bean scopes?
singleton (the default - one shared instance per container), prototype (a new instance on each request), and for web apps request and session scopes tied to the HTTP lifecycle. Most beans are singletons, so keep them stateless.
What Spring Boot adds
How is Spring Boot different from the Spring Framework?
Spring is a comprehensive but configuration-heavy framework. Spring Boot sits on top and removes that friction: auto-configuration, starter dependencies, an embedded server, and sensible defaults let you run a production-ready app with almost no XML or boilerplate. It's Spring with 'convention over configuration'.
What is auto-configuration and how does it work?
Auto-configuration inspects the classpath, existing beans, and properties, and configures beans automatically - e.g. seeing an H2 driver on the classpath, it configures a DataSource. It's driven by @EnableAutoConfiguration (included in @SpringBootApplication) and conditional annotations like @ConditionalOnClass. You override any default just by defining your own bean.
What are Spring Boot starters?
Starters are curated dependency bundles. Adding spring-boot-starter-web pulls in Spring MVC, Jackson and an embedded Tomcat with compatible versions, so you don't hand-pick and align dozens of libraries. There are starters for data-jpa, security, test, and more.
What does @SpringBootApplication do?
It's a convenience annotation combining three: @Configuration (marks the class as a bean source), @EnableAutoConfiguration (turns on auto-configuration), and @ComponentScan (scans the package and sub-packages for components). It's placed on the main class.
Building REST APIs
How do you create a REST endpoint in Spring Boot?
Annotate a class with @RestController (which combines @Controller and @ResponseBody so return values are serialised to JSON), map paths with @RequestMapping or the verb-specific @GetMapping/@PostMapping, bind path segments with @PathVariable, and read the JSON body with @RequestBody.
@Controller vs @RestController?
@Controller returns view names for server-rendered pages (or needs @ResponseBody per method to return data). @RestController is @Controller plus @ResponseBody on every method, so it returns serialised data (JSON) directly - the right choice for REST APIs.
How do you handle exceptions across controllers?
Use @ExceptionHandler methods for a single controller, or a class annotated @ControllerAdvice (or @RestControllerAdvice) to handle exceptions globally and return consistent error responses with appropriate HTTP status codes via ResponseEntity.
How do you validate request data?
Put Bean Validation annotations (@NotNull, @Size, @Email) on the request DTO and add @Valid to the @RequestBody parameter. Spring validates automatically and, on failure, raises a MethodArgumentNotValidException you can handle in @ControllerAdvice.
Data access with Spring Data JPA
What is Spring Data JPA and a repository?
Spring Data JPA is an abstraction over JPA/Hibernate that removes boilerplate DAO code. You declare an interface extending JpaRepository<Entity, Id> and Spring generates the implementation at runtime, giving you CRUD, paging and sorting for free.
How do derived query methods work?
Spring Data parses a repository method's name and generates the query: findByEmail(String email) becomes a query on the email field, findByAgeGreaterThanOrderByNameAsc adds a condition and ordering. For anything complex, use @Query with JPQL or native SQL.
What is the N+1 select problem?
When loading a list of entities and then lazily loading each one's association, you fire one query for the list plus N queries for the associations - N+1 round trips. Fix it with a fetch join, an entity graph, or batch fetching so the data loads in one or a few queries.
How do you manage transactions?
Annotate a service method with @Transactional and Spring wraps it in a transaction that commits on success and rolls back on a runtime exception. Keep transactions at the service layer, and be aware that self-invocation within the same bean bypasses the proxy and its transaction.
How to actually get ready
Reading these answers builds recognition, but interviews test recall under pressure. Practise retrieving, not re-reading: take timed MCQ rounds on dependency injection and auto-configuration first, because those are the concepts interviewers probe hardest, then read the explanation for anything you miss and revisit it the next day.
Spring Boot questions sit on top of core Java, so a shaky answer on collections or exceptions undermines a strong Spring answer. Interleave Java and DBMS practice so you're ready when the interview moves from '@Transactional' to 'and how does a database index make that query fast'.
Practice this now
