Back to IQs

Springboot Interview Questions

What is Spring Boot?
Spring Boot is a framework that simplifies the setup and configuration of Spring applications. It provides production-ready applications with minimal configuration.
What are the advantages of using Spring Boot?
Spring Boot reduces development time by providing built-in configurations, embedded servers, automatic dependency management, and easier setup for microservices.
What is the difference between Spring Boot and Spring MVC?
Spring Boot is a framework for creating stand-alone Spring-based applications, while Spring MVC is a part of the Spring framework used for building web applications.
What is an embedded server in Spring Boot?
Spring Boot provides embedded servers like Tomcat, Jetty, and Undertow, allowing applications to run as standalone Java applications without needing to deploy to an external server.
What is a Spring Boot Starter?
A Spring Boot Starter is a set of pre-configured dependencies that help you quickly add features like web, JPA, security, etc., to a Spring Boot project.
What is Spring Boot Actuator?
Spring Boot Actuator provides production-ready features like monitoring, metrics, health checks, and application configuration management.
How do you configure application properties in Spring Boot?
You can configure application properties in the 'application.properties' or 'application.yml' file, which can define various settings like server port, database configurations, etc.
What is the purpose of @SpringBootApplication annotation?
The @SpringBootApplication annotation is a combination of @EnableAutoConfiguration, @ComponentScan, and @Configuration annotations, and it marks the main class of a Spring Boot application.
What is a Spring Boot profile?
A Spring Boot profile allows you to define different configurations for different environments (e.g., dev, prod) in the application.properties file or as command-line arguments.
What is the role of @RestController in Spring Boot?
@RestController is a convenience annotation that combines @Controller and @ResponseBody, making it easier to create RESTful APIs by automatically serializing responses to JSON.
How do you handle exceptions in Spring Boot?
In Spring Boot, exceptions can be handled using @ControllerAdvice to handle global exceptions or using @ExceptionHandler to handle exceptions in specific controllers.
What is Spring Data JPA?
Spring Data JPA is a part of the Spring Data project that simplifies database operations using JPA (Java Persistence API) by providing repository support for CRUD operations.
What is Spring Security?
Spring Security is a powerful and customizable authentication and authorization framework for Java applications, offering support for both web and method-level security.
What are Spring Boot profiles used for?
Spring Boot profiles are used to define different configurations for different environments, such as development, testing, and production.
What is the difference between @Component, @Service, @Repository, and @Controller?
@Component is a generic stereotype annotation, while @Service, @Repository, and @Controller are specialized forms of @Component used for service layers, persistence layers, and web controllers, respectively.
What is the use of @Entity in Spring Boot?
@Entity is an annotation that marks a class as a persistent entity, meaning it will be mapped to a database table.
What is Spring Boot Auto Configuration?
Spring Boot Auto Configuration automatically configures Spring beans based on the libraries present in the classpath, simplifying the configuration process.
How do you set up logging in Spring Boot?
Logging in Spring Boot can be configured using the 'application.properties' file with log levels like 'INFO', 'DEBUG', or 'ERROR', and you can use logging frameworks like Logback or SLF4J.
What is @ConfigurationProperties in Spring Boot?
@ConfigurationProperties is used to bind properties from configuration files (like application.properties) to Java beans for better separation of configuration.
How can you test Spring Boot applications?
Spring Boot applications can be tested using tools like JUnit and Spring Test, with annotations like @SpringBootTest for integration tests and @MockBean for mocking beans.
What is Spring Boot DevTools?
Spring Boot DevTools is a set of tools that improves the development experience by providing features like automatic restarts and live reload.
What changed when moving from Spring Boot 2 to Spring Boot 3?
Spring Boot 3 requires Java 17 or later and uses Jakarta EE namespaces, so javax imports must generally become jakarta imports. It also aligns with newer Spring Framework and observability APIs, making dependency and compatibility checks important during migration.
What does @Transactional provide, and what are common pitfalls?
@Transactional defines transaction boundaries and coordinates commit or rollback through a transaction manager. Common pitfalls include self-invocation bypassing the proxy, catching exceptions without rethrowing them, long-running transactions, and assuming it makes remote service calls atomic.
How should a Spring Boot REST API validate requests?
Use Bean Validation annotations on request DTOs with @Valid or @Validated, return a consistent error contract through centralized exception handling, and validate business invariants in the service or domain layer. Validation at the controller boundary is not a replacement for authorization or database constraints.
How do you secure a Spring Boot API using OAuth2 or JWT?
Treat the API as a resource server, validate token signature, issuer, audience, expiry, and required scopes or authorities, then enforce authorization at the endpoint and service boundaries. Do not trust claims without verification, put secrets in a secret manager, and prefer short-lived access tokens with a deliberate refresh strategy.
What should a production Spring Boot service expose for observability?
Expose health checks that distinguish liveness from readiness, collect metrics such as latency, error rate, traffic, and saturation, and emit structured logs with correlation or trace IDs. Protect actuator endpoints, avoid sensitive data in logs, and use distributed tracing to follow requests across dependencies.
How does Spring Boot support containerized deployment?
A Spring Boot service should externalize configuration, log to standard output, expose readiness and liveness checks, handle graceful shutdown, and set resource-aware JVM and container limits. Build a small, repeatable image, scan dependencies and the image, and make startup and health behavior observable in the target platform.
What is Testcontainers used for in Spring Boot testing?
Testcontainers starts real disposable dependencies such as PostgreSQL, Kafka, or Redis in containers for integration tests. It catches schema, serialization, and configuration issues that mocks cannot, while unit tests should still cover fast business-logic checks independently.
How do you prevent N+1 queries in a Spring Data JPA application?
Detect the query pattern with SQL logging or metrics, then fetch only the needed graph using a projection, fetch join, entity graph, or explicit query. Avoid making every relationship EAGER; that can create oversized joins and hidden work. Verify the fix with realistic data and query-count tests.