Advanced Java & Spring Boot · Lesson 16 of 16 · Updated ·
Spring Boot and Microservices Interview Questions — With Short Answers
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!).
Spring core and Spring Boot
- What is dependency injection? The container creates objects and passes their dependencies in (usually through the constructor), so classes stay loosely coupled and testable.
- Bean scopes? singleton (default), prototype, request, session.
- @Component vs @Bean?
@Componenton your own class (found by scanning);@Beanon a factory method in a@Configurationclass (for library classes). - Two beans of one type? Use
@Primaryfor the default and@Qualifier("name")to pick one. - What does @SpringBootApplication do? Component scan + auto-configuration + configuration.
- What is auto-configuration? Boot creates beans based on the classpath and properties; your own beans or properties override it.
- How do you change config per environment? Profiles (
application-prod.properties,--spring.profiles.active=prod) and environment variables. - Spring vs Spring Boot? Spring is the framework; Boot adds starters, auto-configuration, an embedded server and production features.
REST
- PUT vs PATCH? PUT replaces the resource; PATCH changes some fields.
- 401 vs 403? 401 = not authenticated; 403 = authenticated but not allowed.
- @Controller vs @RestController?
@RestController=@Controller+@ResponseBody(return values become JSON). - @PathVariable vs @RequestParam?
/courses/{id}vs/courses?maxFee=3000. - How do you validate input? Bean Validation annotations on the DTO +
@Valid; handle errors in@RestControllerAdvice. - Why DTOs instead of entities? Control what's exposed, avoid lazy-loading and recursion issues, keep the API stable.
JPA, Hibernate and transactions
- JPA vs Hibernate? JPA is the specification; Hibernate is the most-used implementation.
- 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.
- LAZY vs EAGER? LAZY loads on first access; EAGER loads immediately. Prefer LAZY and fetch what you need.
- save() vs dirty checking? Managed entities are saved automatically at commit;
save()is needed for new entities. - 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.
- Optimistic vs pessimistic locking?
@Versioncolumn checked at update (conflicts rare) vs locking rows while reading (conflicts frequent).
Security
- 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. - Where do you store passwords? Hashed with bcrypt/argon2, never plain or MD5.
- When do you disable CSRF? For stateless token APIs without cookies; keep it for cookie-based web apps.
Testing
- @WebMvcTest vs @SpringBootTest? Web slice with mocked services vs the whole application context.
- @Mock vs @MockitoBean?
@Mockin plain Mockito tests;@MockitoBeanreplaces a bean inside a Spring test context.
Microservices
- Why microservices? Independent deployment, scaling and team ownership — at the cost of network complexity.
- Service discovery? Services register by name (Eureka, Kubernetes Services) and callers look them up.
- What does an API gateway do? Single entry point: routing, auth, rate limiting, CORS.
- Circuit breaker? Stops calling a failing service (OPEN), uses a fallback, tests again after a while (HALF-OPEN).
- Synchronous vs asynchronous communication? REST/Feign when you need an answer now; events (Kafka) to notify others and stay decoupled.
- 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.
- Kafka consumer group? Instances sharing a topic's partitions; each group receives every event.
Docker
- Image vs container? Template vs running instance.
- 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.