Spring Boot remains one of the most widely used Java frameworks in Indian enterprise and product companies. TCS, Infosys, Wipro, HCL, and most Indian BFSI tech teams build their backend services in Spring Boot. Even at product companies like Flipkart, PhonePe, and Razorpay, Java with Spring Boot powers critical payment and order services. This guide covers the Spring Boot questions most commonly asked in Indian technical interviews.
Spring Core and IoC container
Q: What is Spring IoC and dependency injection? Inversion of Control (IoC) is a design principle where the framework controls the flow of the programme rather than the programme controlling it. Spring's IoC container manages object creation and dependency wiring. Dependency injection means the container 'injects' dependencies into objects rather than the object creating them. Result: loosely coupled code that is easier to test.
Q: What is the difference between @Component, @Service, @Repository, and @Controller? All four are specialisations of @Component and register the class as a Spring bean. @Service marks business logic classes. @Repository marks data access classes and adds exception translation. @Controller marks MVC web controllers. These annotations are semantically meaningful: they document intent: but functionally they behave the same way.
Q: What is the difference between @Autowired and constructor injection? @Autowired on a field works but makes testing difficult (cannot inject mocks without Spring context). Constructor injection is preferred: it makes dependencies explicit, allows final fields, and works without any annotation in Spring 4.3+ (single constructor). Use constructor injection by default.
Q: What is @Bean vs @Component? @Component is class-level, used on your own classes. @Bean is method-level, used inside @Configuration classes to define beans for third-party classes you cannot annotate (like library classes). Both register beans in the Spring container.
Spring Boot auto-configuration and REST
Q: How does Spring Boot auto-configuration work? Spring Boot's @SpringBootApplication annotation includes @EnableAutoConfiguration. At startup, Spring Boot reads META-INF/spring.factories (in Spring Boot 2) or META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (in Spring Boot 3) and conditionally applies configuration classes based on what is on the classpath. Add spring-boot-starter-web → auto-configures DispatcherServlet, Jackson, Tomcat. Add spring-boot-starter-data-jpa → auto-configures EntityManager, DataSource.
Q: What is the difference between @RestController and @Controller? @RestController = @Controller + @ResponseBody on every method. Every method return value is serialised to JSON/XML directly, not passed to a view resolver. Use @RestController for REST APIs. Use @Controller when returning view names (Thymeleaf, JSP).
Q: What are the common HTTP status codes and when to return each? 200 OK (success with body), 201 Created (resource created: include Location header), 204 No Content (success, no body: DELETE), 400 Bad Request (client error, invalid input), 401 Unauthorised (not authenticated), 403 Forbidden (authenticated but not authorised), 404 Not Found (resource does not exist), 409 Conflict (state conflict: duplicate creation), 422 Unprocessable Entity (validation error), 429 Too Many Requests (rate limited), 500 Internal Server Error (unhandled server error).
Q: How do you handle exceptions globally in Spring Boot? @ControllerAdvice with @ExceptionHandler methods. A single class handles exceptions from all controllers and maps them to appropriate HTTP responses. Use @RestControllerAdvice (combines @ControllerAdvice + @ResponseBody) for REST APIs.
Spring Data JPA and database
Q: What is Spring Data JPA and how does it differ from plain Hibernate? Spring Data JPA provides a repository abstraction on top of JPA (which Hibernate implements). Extend JpaRepository<Entity, Id> and Spring Boot auto-generates implementations for common operations (findById, save, delete, findAll). You can also write query methods from method names (findByEmailAndStatus) or use @Query for JPQL/SQL. Hibernate is the ORM layer; Spring Data JPA is the convenience layer on top.
Q: What is lazy vs eager loading in JPA? Lazy loading: related entities are not loaded until accessed. Eager loading: related entities are always loaded with the parent. Default: @ManyToOne and @OneToOne are EAGER by default. @OneToMany and @ManyToMany are LAZY. N+1 problem: accessing a lazy collection in a loop triggers N additional queries. Solution: use JOIN FETCH in JPQL or @EntityGraph to fetch in one query.
Q: What is a JPA transaction and how do you manage it in Spring? @Transactional marks a method or class for transactional execution. Spring wraps the call in a transaction that commits on success and rolls back on RuntimeException. Propagation types: REQUIRED (join existing or create new: default), REQUIRES_NEW (always create a new transaction), NEVER (must not run in a transaction). Read-only transactions: @Transactional(readOnly = true) for query-only methods: prevents unnecessary dirty checking.
Q: What are database connection pools and how does Spring Boot configure them? Connection pools maintain a set of reusable database connections. Spring Boot uses HikariCP by default: the fastest connection pool in the JVM. Key configuration: spring.datasource.hikari.maximum-pool-size (default 10: tune for production). A pool size too small causes connection waiting. Too large exhausts database connections.
Spring Boot questions come up in almost every Java backend interview in India. Practise explaining IoC, REST design, and database transactions clearly with HireStepX's AI mock interviewer.
Practice freeSpring Security and Spring Cloud
Q: How do you implement JWT authentication in Spring Boot? Add Spring Security and java-jwt. Write a JwtFilter that extracts and validates the JWT from Authorization header. Register it before UsernamePasswordAuthenticationFilter. On login, generate JWT with user claims and return it. For each request, validate the token and set SecurityContextHolder.
Q: What is Spring Cloud and when do you need it? Spring Cloud provides tools for building distributed systems and microservices: Eureka (service discovery), Ribbon/LoadBalancer (client-side load balancing), Feign (declarative REST client), Config Server (externalised configuration), Gateway (API gateway), Sleuth + Zipkin (distributed tracing). At Indian product companies, microservices at scale often use a subset of these.
Q: What is circuit breaking in Spring and how is it implemented? Use Resilience4j (the modern replacement for Hystrix). @CircuitBreaker annotation on a method opens the circuit when the failure rate exceeds a threshold, returning a fallback method's result instead of blocking. The circuit half-opens after a wait period to test recovery.
Frequently asked questions
Practice these questions on HireStepX