# Spring Boot and Microservices Interview Questions — With Short Answers

Source: https://skills101.in/learn/spring-boot/spring-boot-interview-questions — Skills101 free tutorial (Advanced Java & Spring Boot). Updated 2026-11-09.

Revise these before Java backend interviews. Each answer is short on purpose — say it, then give an example from **your** project (the Course API counts!).

![The Spring Boot track at a glance: Core Java, JDBC, Maven, Spring Core, Spring Boot, REST, JPA, Security, Testing, Microservices and Docker](https://skills101.in/learn-img/spring-boot/roadmap.svg)

## Spring core and Spring Boot

1. **What is dependency injection?** The container creates objects and passes their dependencies in (usually through the constructor), so classes stay loosely coupled and testable.
2. **Bean scopes?** singleton (default), prototype, request, session.
3. **@Component vs @Bean?** `@Component` on your own class (found by scanning); `@Bean` on a factory method in a `@Configuration` class (for library classes).
4. **Two beans of one type?** Use `@Primary` for the default and `@Qualifier("name")` to pick one.
5. **What does @SpringBootApplication do?** Component scan + auto-configuration + configuration.
6. **What is auto-configuration?** Boot creates beans based on the classpath and properties; your own beans or properties override it.
7. **How do you change config per environment?** Profiles (`application-prod.properties`, `--spring.profiles.active=prod`) and environment variables.
8. **Spring vs Spring Boot?** Spring is the framework; Boot adds starters, auto-configuration, an embedded server and production features.

## REST

9. **PUT vs PATCH?** PUT replaces the resource; PATCH changes some fields.
10. **401 vs 403?** 401 = not authenticated; 403 = authenticated but not allowed.
11. **@Controller vs @RestController?** `@RestController` = `@Controller` + `@ResponseBody` (return values become JSON).
12. **@PathVariable vs @RequestParam?** `/courses/{id}` vs `/courses?maxFee=3000`.
13. **How do you validate input?** Bean Validation annotations on the DTO + `@Valid`; handle errors in `@RestControllerAdvice`.
14. **Why DTOs instead of entities?** Control what's exposed, avoid lazy-loading and recursion issues, keep the API stable.

## JPA, Hibernate and transactions

15. **JPA vs Hibernate?** JPA is the specification; Hibernate is the most-used implementation.
16. **What is the N+1 problem?** One query for a list + one per row for a lazy relationship; fix with JOIN FETCH or entity graphs.
17. **LAZY vs EAGER?** LAZY loads on first access; EAGER loads immediately. Prefer LAZY and fetch what you need.
18. **save() vs dirty checking?** Managed entities are saved automatically at commit; `save()` is needed for new entities.
19. **How does @Transactional work?** A proxy starts a transaction before the method and commits or rolls back after; rolls back on unchecked exceptions; self-calls bypass it.
20. **Optimistic vs pessimistic locking?** `@Version` column checked at update (conflicts rare) vs locking rows while reading (conflicts frequent).

## Security

21. **How does JWT authentication work?** Login returns a signed token; clients send `Authorization: Bearer …`; the server verifies the signature and expiry on every request — no session.
22. **Where do you store passwords?** Hashed with bcrypt/argon2, never plain or MD5.
23. **When do you disable CSRF?** For stateless token APIs without cookies; keep it for cookie-based web apps.

## Testing

24. **@WebMvcTest vs @SpringBootTest?** Web slice with mocked services vs the whole application context.
25. **@Mock vs @MockitoBean?** `@Mock` in plain Mockito tests; `@MockitoBean` replaces a bean inside a Spring test context.

## Microservices

26. **Why microservices?** Independent deployment, scaling and team ownership — at the cost of network complexity.
27. **Service discovery?** Services register by name (Eureka, Kubernetes Services) and callers look them up.
28. **What does an API gateway do?** Single entry point: routing, auth, rate limiting, CORS.
29. **Circuit breaker?** Stops calling a failing service (OPEN), uses a fallback, tests again after a while (HALF-OPEN).
30. **Synchronous vs asynchronous communication?** REST/Feign when you need an answer now; events (Kafka) to notify others and stay decoupled.
31. **How do you keep data consistent across services?** Each service owns its data; use events and the Saga pattern; the outbox pattern for reliable publishing.
32. **Kafka consumer group?** Instances sharing a topic's partitions; each group receives every event.

## Docker

33. **Image vs container?** Template vs running instance.
34. **Why multi-stage builds?** Build with the JDK, ship only a JRE and the jar.

## How to answer well

- Use the **project** you built: "In my Course API, I used a join entity for enrolments and JOIN FETCH to avoid N+1."
- Admit trade-offs: "Microservices add network failures, so I added a timeout, a circuit breaker and a fallback."
- Put the four example projects on your GitHub with a README, and you have a portfolio piece.

## Common mistakes

- Memorising definitions without an example from your own code.
- Claiming microservices are always better.
- Mixing up 401/403, JPA/Hibernate, or @Mock/@MockitoBean.

## Quick recap

- Core: DI, scopes, @Primary/@Qualifier, auto-configuration, profiles.
- Data: N+1, LAZY, dirty checking, @Transactional rules, locking.
- Security: JWT flow, bcrypt, CSRF. Testing: slices vs full context.
- Microservices: discovery, gateway, Feign + circuit breaker, Kafka, Docker.


Practice quiz and free certificate: https://skills101.in/learn/spring-boot/spring-boot-interview-questions