Spring Boot Interview Questions for Experienced Devs

Hero Image

Spring Boot has revolutionized Java development by eliminating boilerplate code and providing opinionated defaults. For experienced developers, interviews go beyond basic @RestController annotations; interviewers expect a deep understanding of internal mechanisms, performance tuning, security, and microservices architecture.

In this guide, we dive into the most challenging Spring Boot interview questions tailored for senior engineers. Let's explore the intricacies of this powerful framework.

1. How does Spring Boot Auto-Configuration work under the hood?

Auto-configuration is the magic behind Spring Boot, but experienced developers need to explain the "how."

Spring Boot looks at the classpath, existing beans, and various property settings to conditionally configure beans. This is driven by the @EnableAutoConfiguration annotation, which imports AutoConfigurationImportSelector.

This selector reads the META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (in Spring Boot 2.7+) or META-INF/spring.factories (in older versions) to find auto-configuration classes. These classes use @Conditional annotations (like @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty) to determine if their beans should be created.

Follow-up Question: How can you disable a specific auto-configuration? Answer: You can use the exclude attribute on @SpringBootApplication or @EnableAutoConfiguration, or use the spring.autoconfigure.exclude property in your application.properties/application.yml.

Diagram

2. Explain the difference between @Component, @Service, @Repository, and @Controller. When should you use which?

While they all make a class a Spring-managed bean, they serve different semantic purposes and offer specific features.

  • @Component: The generic stereotype for any Spring-managed component. Use it when a class doesn't fall into the other specific categories.
  • @Repository: Indicates that the class provides the mechanism for storage, retrieval, search, update, and delete operation on objects. Crucially, it enables automatic translation of vendor-specific data access exceptions into Spring's unified DataAccessException hierarchy.
  • @Service: Indicates that an annotated class is a "Service", originally defined by Domain-Driven Design as "an operation offered as an interface that stands alone in the model, with no encapsulated state." Currently, it doesn't provide additional behavior over @Component but clarifies intent.
  • @Controller: Indicates that the annotated class is a web controller, typically used with @RequestMapping. Spring's component scanner automatically detects these.

3. What is the Spring IoC Container and how does Dependency Injection work?

Inversion of Control (IoC) is a principle where the control of object creation and lifecycle is transferred from the application code to a framework (the container). Spring's IoC container is responsible for instantiating, configuring, and assembling beans based on configuration metadata (XML, annotations, or Java code).

Dependency Injection (DI) is the pattern used to implement IoC. Instead of an object creating its dependencies, the container "injects" them at runtime.

Types of Dependency Injection:

  1. Constructor Injection: Dependencies are provided through the class constructor. (Recommended for mandatory dependencies).
  2. Setter Injection: Dependencies are provided through setter methods. (Useful for optional dependencies).
  3. Field Injection: Dependencies are injected directly into fields using @Autowired. (Generally discouraged as it makes unit testing harder without a Spring context).

Illustration

4. How do you handle exceptions globally in a Spring Boot REST API?

Senior developers are expected to implement clean, consistent error handling.

The standard approach is using @ControllerAdvice (or @RestControllerAdvice) along with @ExceptionHandler.

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleResourceNotFound(ResourceNotFoundException ex, WebRequest request) {
        ErrorResponse error = new ErrorResponse(
            HttpStatus.NOT_FOUND.value(),
            ex.getMessage(),
            LocalDateTime.now()
        );
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleGlobalException(Exception ex, WebRequest request) {
        ErrorResponse error = new ErrorResponse(
            HttpStatus.INTERNAL_SERVER_ERROR.value(),
            "An unexpected error occurred",
            LocalDateTime.now()
        );
        return new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

This creates a centralized point for handling specific exceptions and returning structured JSON error responses.

5. What are Spring Boot Actuator endpoints and how do you secure them?

Spring Boot Actuator provides built-in endpoints for monitoring and managing your application (e.g., /actuator/health, /actuator/metrics, /actuator/env, /actuator/beans).

Because these endpoints expose sensitive application data and operations, securing them is paramount.

How to secure them:

  1. Expose selectively: By default, only /health and /info are exposed over HTTP. You must explicitly expose others using management.endpoints.web.exposure.include=* (or specific ones).
  2. Spring Security: Use Spring Security to restrict access to actuator endpoints based on roles.
  3. Different Port: Configure actuator endpoints to run on a different HTTP port than the main application (management.server.port), which can be firewalled off from public access.

Conclusion

Mastering Spring Boot for senior roles requires looking under the hood. Understanding auto-configuration, robust exception handling, application monitoring, and the nuances of the IoC container will set you apart.

If you are preparing for your next big technical interview, practice these concepts thoroughly. For more interview preparation resources and mock interviews, check out totop.app, where you can simulate real interview environments and get immediate feedback. Good luck!