Spring Boot Interview Questions and Answers for 8+ Years Experience
Core Spring Boot
Section titled “Core Spring Boot”Why Spring Boot instead of Spring Framework?
Section titled “Why Spring Boot instead of Spring Framework?”Spring Boot simplifies development by reducing configuration.
Advantages
- Auto Configuration
- Embedded servers (Tomcat, Jetty)
- Starter dependencies
- Production-ready features
- Minimal XML configuration
@SpringBootApplication
Section titled “@SpringBootApplication”It combines:
@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application {}More precisely, it’s meta-annotated with @SpringBootConfiguration (which is itself annotated with @Configuration), @EnableAutoConfiguration, and @ComponentScan:
flowchart TD
A["@SpringBootApplication"]
A --> B["@SpringBootConfiguration"]
A --> C["@EnableAutoConfiguration"]
A --> D["@ComponentScan"]
Purpose:
- Enables configuration
- Enables auto configuration
- Performs component scanning
Auto Configuration
Section titled “Auto Configuration”Auto configuration creates beans automatically based on classpath dependencies.
Example:
If spring-boot-starter-data-jpa is present, Spring Boot configures:
- DataSource
- EntityManager
- Hibernate
flowchart LR
A[Application Starts] --> B[Read Classpath]
B --> C{Dependency Found?}
C -->|JPA| D[Configure DataSource]
C -->|Web| E[Configure Tomcat & MVC]
D --> F[Application Ready]
E --> F
Spring Boot Starters
Section titled “Spring Boot Starters”| Starter | Purpose |
|---|---|
| spring-boot-starter-web | Web APIs |
| spring-boot-starter-data-jpa | JPA/Hibernate |
| spring-boot-starter-security | Security |
| spring-boot-starter-test | Testing |
| spring-boot-starter-validation | Bean Validation (Hibernate Validator) |
| spring-boot-starter-actuator | Monitoring and health endpoints |
| spring-boot-starter-cache | Caching with Redis/Ehcache |
| spring-boot-starter-amqp | Messaging with RabbitMQ |
spring-boot-starter-kafka (from spring-kafka) |
Messaging with Kafka |
Bean and Application Context
Section titled “Bean and Application Context”Bean Scopes
Section titled “Bean Scopes”| Scope | Description |
|---|---|
| Singleton | One instance per container |
| Prototype | New instance every request |
| Request | One per HTTP request |
| Session | One per session |
| Application | One per servlet context |
@Scope("prototype")@Componentclass MyBean {}flowchart TD
A[Bean Request] --> B{Scope}
B -->|Singleton| C[Reuse one container instance]
B -->|Prototype| D[Create new instance]
B -->|Request| E[One per HTTP request]
B -->|Session| F[One per HTTP session]
B -->|Application| G[One per ServletContext]
Spring Singleton vs Java Singleton
Section titled “Spring Singleton vs Java Singleton”| Feature | Spring Singleton | Plain Java Singleton |
|---|---|---|
| Scope | Per Spring container | Per JVM |
| Creation | Managed by Spring | Manual |
| Lifecycle | Spring-managed | Developer-managed |
| Dependency Injection | Supported | Not supported |
Plain Java singleton example:
class Singleton {
private static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton getInstance() { return instance; }}Resolving Bean Ambiguity
Section titled “Resolving Bean Ambiguity”Using @Qualifier
@Autowired@Qualifier("emailService")private NotificationService service;Using @Primary
@Primary@Componentclass EmailService {}@PostConstruct
Section titled “@PostConstruct”@PostConstructpublic void init() { System.out.println("Bean initialized");}Common use cases: cache initialization, loading reference data, warming database connections, preloading configuration.
REST APIs
Section titled “REST APIs”Creating REST APIs
Section titled “Creating REST APIs”@RestController@RequestMapping("/products")public class ProductController {
@GetMapping public List<Product> getProducts() { return productService.getProducts(); }}Common annotations used to build the controller:
| Annotation | Purpose |
|---|---|
@RestController |
Marks a class as a REST controller |
@RequestMapping |
Base URL mapping |
@GetMapping |
Handles HTTP GET requests |
@PostMapping |
Handles HTTP POST requests |
@PutMapping |
Handles HTTP PUT requests |
@DeleteMapping |
Handles HTTP DELETE requests |
Request flow through the layers:
flowchart LR A[HTTP Request] --> B[DispatcherServlet] B --> C[REST Controller] C --> D[Service Layer] D --> E[Repository] E --> D D --> C C --> F[JSON Response]
Request Mapping Annotations
Section titled “Request Mapping Annotations”| Feature | @RequestParam |
@PathVariable |
@RequestBody |
|---|---|---|---|
| Data Location | Query parameters | URL path | Request body |
| Typical Use | Filtering, optional values | Resource identification | Sending objects |
| Example URL | /products?type=mobile |
/products/101 |
JSON payload |
| Common HTTP Methods | Mostly GET | GET/PUT/DELETE | POST/PUT |
@GetMapping("/products")public List<Product> getProducts(@RequestParam String type)@GetMapping("/products/{id}")public Product getProduct(@PathVariable Long id)@PostMapping("/products")public Product createProduct(@RequestBody Product product)Global Exception Handling
Section titled “Global Exception Handling”@ControllerAdvicepublic class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception ex) { return ResponseEntity.status(500).body(ex.getMessage()); }}flowchart LR A[Controller] -->|Throws Exception| B[ControllerAdvice] B --> C[@ExceptionHandler] C --> D[ResponseEntity]
| Feature | @ControllerAdvice |
@ExceptionHandler |
|---|---|---|
| Purpose | Global exception handling | Handles a specific exception |
| Scope | All controllers | Individual handler method |
| Level | Class | Method |
Spring Data JPA
Section titled “Spring Data JPA”Hibernate vs JPA vs Spring Data JPA
Section titled “Hibernate vs JPA vs Spring Data JPA”| Feature | JPA | Hibernate | Spring Data JPA |
|---|---|---|---|
| Type | Specification | Implementation | Framework built on JPA |
| Developed by | Java | Hibernate team | Spring |
| Purpose | Defines ORM standard | Implements ORM | Simplifies data access |
| Query Support | JPQL | HQL | Method names, JPQL, Native |
| Boilerplate | More code | Medium | Very little |
| Example | EntityManager |
SessionFactory |
JpaRepository |
flowchart TD
A[Spring Data JPA] --> B[JPA Specification]
B --> C[Hibernate]
C --> D[(Database)]
The same save operation at each layer:
// JPAEntityManager.persist(employee);
// Hibernatesession.save(employee);
// Spring Data JPAemployeeRepository.save(employee);Lazy vs Eager Fetching
Section titled “Lazy vs Eager Fetching”@OneToMany(fetch = FetchType.LAZY)private List<Order> orders;| Fetch Type | Description |
|---|---|
| Lazy | Load related entities only when accessed |
| Eager | Load related entities immediately |
Default fetch types by relationship:
| Relationship | Default |
|---|---|
@ManyToOne |
EAGER |
@OneToMany |
LAZY |
Derived Queries
Section titled “Derived Queries”List<User> findByAgeGreaterThan(int age);See the JPA persistence fundamentals guide for more derived query examples, common keyword combinations, and the JPQL/native query alternative.
Transaction Management
Section titled “Transaction Management”ACID Properties
Section titled “ACID Properties”- Atomicity
- Consistency
- Isolation
- Durability
@Transactionalpublic void transfer() {}Propagation
Section titled “Propagation”| Type | Description |
|---|---|
| REQUIRED | Join existing transaction |
| REQUIRES_NEW | New transaction |
| SUPPORTS | Use existing if present |
| NOT_SUPPORTED | No transaction |
| MANDATORY | Must run inside an existing transaction |
| NEVER | Must not run inside a transaction |
| NESTED | Create a nested transaction |
Why @Transactional Doesn’t Work on Private Methods
Section titled “Why @Transactional Doesn’t Work on Private Methods”Two separate issues combine here:
- Private methods can’t be proxied at all. CGLIB (subclass) proxies can’t override
privatemethods, so@Transactionalon a private method is silently ignored regardless of how it’s called. - Self-invocation bypasses the proxy, even for
publicmethods. CallingsaveOrder()from inside the same class resolves tothis.saveOrder()— the raw object — not the proxy wrapping it. So even ifsaveOrder()werepublic, calling it internally fromplaceOrder()would still skip the transactional behavior; only calls arriving through the proxy from outside the class trigger it.
@Servicepublic class OrderService {
public void placeOrder() { saveOrder(); }
@Transactional private void saveOrder() { // Transaction will NOT start - private methods aren't proxied, // and this is also a self-invoked call either way. }}Isolation Levels
Section titled “Isolation Levels”Isolation levels exist to prevent these concurrency problems, in increasing order of strictness:
| Problem | Description |
|---|---|
| Dirty Read | Reading uncommitted data |
| Non-repeatable Read | Same query returns different values |
| Phantom Read | Additional rows appear in repeated queries |
| Isolation | Description |
|---|---|
| READ_UNCOMMITTED | Dirty reads allowed |
| READ_COMMITTED | Reads committed data only |
| REPEATABLE_READ | Prevents non-repeatable reads |
| SERIALIZABLE | Highest isolation |
@Transactional(isolation = Isolation.REPEATABLE_READ)Typical defaults: MySQL uses REPEATABLE_READ; PostgreSQL uses READ_COMMITTED.
Transaction Best Practices
Section titled “Transaction Best Practices”- Use
@Transactionalin the service layer, not in controllers. - Prefer
@Transactional(readOnly = true)for read operations. - Configure rollback rules explicitly when needed.
@Transactional(readOnly = true)@Transactional(rollbackFor = Exception.class)Microservices
Section titled “Microservices”Service Discovery
Section titled “Service Discovery”sequenceDiagram participant Service participant Eureka participant Client Service->>Eureka:Register Client->>Eureka:Lookup Service Eureka-->>Client:Instance Details Client->>Service:Invoke API
API Gateway Responsibilities
Section titled “API Gateway Responsibilities”- Routing
- Authentication
- Logging
- Rate limiting
Production Features
Section titled “Production Features”Spring Boot Actuator
Section titled “Spring Boot Actuator”Common endpoints:
/actuator/health/actuator/metrics/actuator/info
Debugging Production Issues
Section titled “Debugging Production Issues”- Capture request
- Reproduce locally
- Enable logs
- Debug
- Analyze stack trace
logging.level.root=DEBUGSpring Security
Section titled “Spring Security”Authentication vs Authorization
Section titled “Authentication vs Authorization”| Authentication | Authorization |
|---|---|
| Identity verification | Access control |
JWT Flow
Section titled “JWT Flow”sequenceDiagram participant User participant Server User->>Server: Login Server-->>User: JWT Token User->>Server: Bearer Token Server-->>User: Protected Resource
Senior-Level Spring Boot Interview Questions (Not Covered in Basics)
Section titled “Senior-Level Spring Boot Interview Questions (Not Covered in Basics)”Spring Boot Internals
Section titled “Spring Boot Internals”- How does auto configuration determine which configuration classes to load?
- Explain
@ConditionalOnClass,@ConditionalOnMissingBean, and@ConditionalOnProperty. - What happens internally during
SpringApplication.run()? - What is
ApplicationContextInitializer? - Explain Spring Boot application events.
- How do you create a custom Spring Boot starter?
Configuration
Section titled “Configuration”application.propertiesvsapplication.yml- Externalized configuration
@ConfigurationPropertiesvs@Value- Dynamic configuration refresh
- Spring Cloud Config
- Multiple active profiles
Performance
Section titled “Performance”- Reduce startup time
- Reduce memory footprint
- Optimize connection pools
- Solve
LazyInitializationException - Scale to millions of requests
- Thread pool tuning
Caching
Section titled “Caching”@Cacheable@CachePut@CacheEvict- Redis integration
- Distributed caching
- Cache stampede prevention
Advanced Microservices
Section titled “Advanced Microservices”- Synchronous vs asynchronous communication
- Saga pattern
- API composition
- Idempotency
- Kafka duplicate message handling
- Centralized logging
Testing
Section titled “Testing”@SpringBootTest@WebMvcTest@DataJpaTest- Integration testing
- Mocking dependencies
- Kafka testing
Observability
Section titled “Observability”- Prometheus
- Grafana
- Spring Boot Actuator metrics
- Distributed tracing
- Spring Cloud Sleuth
Deployment
Section titled “Deployment”- Kubernetes deployment
- Readiness vs Liveness probes
- Zero downtime deployment
- CI/CD pipelines
System Design
Section titled “System Design”- Order Management System
- Payment Service
- Notification Service
- Large File Upload Service
- API Rate Limiter
Project-Based Questions
Section titled “Project-Based Questions”- Explain your architecture.
- Biggest production issue solved.
- Performance improvements delivered.
- Most difficult debugging experience.
- High availability design decisions.
Interview Answering Strategy
Section titled “Interview Answering Strategy”Use this structure:
- Definition
- Internal working
- Practical example
- Real-world use case
Key Takeaways
Section titled “Key Takeaways”- Senior interviews focus heavily on internals, architecture, and production scenarios.
- Master auto configuration, transactions, JPA performance, caching, and observability.
- Be prepared to explain real project decisions and trade-offs.
- Practice system design and microservices patterns alongside Spring Boot fundamentals.