Spring Boot Interview Questions
Spring Boot Interview Questions
Spring Boot is an open-source Java framework designed to simplify the development of stand-
alone, production-grade Spring-based applications. It provides a streamlined experience by
offering auto-configuration, starter dependencies, and an embedded HTTP server, allowing
developers to quickly set up and run Spring applications with minimal configuration.
The main advantages of using Spring Boot include rapid development due to its
convention-over-configuration approach, simplified dependency management, built-in
support for various technologies such as embedded servers and databases, seamless
integration with Spring ecosystem components, and enhanced productivity through
features like auto-configuration and starters.
SUMATHI R 1
6. What is the purpose of the @SpringBootApplication annotation in a Spring Boot
application?
7. Some common annotations used in Spring Boot along with their uses.
1. **@SpringBootApplication**:
2. **@Controller**:
- Use: Marks a class as a Spring MVC controller. It handles incoming web requests and
returns the appropriate response.
3. **@RestController**:
- Use: Like `@Controller`, but specifically tailored for RESTful web services. It
combines `@Controller` and `@ResponseBody`, indicating that methods return data
to be serialized directly into the response body.
4. **@Service**:
5. **@Repository**:
- Use: Marks a class as a repository component, typically used for data access
operations. It facilitates interaction with databases or other external data sources.
6. **@Component**:
SUMATHI R 2
7. **@Autowired**:
8. **@Value**:
- Use: Injects a value from properties files, environment variables, or other sources
into a Spring bean's field, constructor, or method parameter.
9. **@Configuration**:
- Use: Indicates that a class provides bean definitions and should be processed by the
Spring IoC container to generate bean definitions at runtime.
10. **@EnableAutoConfiguration**:
11. **@RequestMapping**:
12. **@PathVariable**:
- Use: Binds a method parameter to a value extracted from the URI path in a Spring
MVC controller.
13. **@RequestParam**:
14. **@ResponseBody**:
- Use: Indicates that a method return value should be serialized directly into the
response body in a Spring MVC controller, typically used with `@RestController`.
15. **@ExceptionHandler**:
- Use: Defines methods to handle specific exceptions globally within a Spring MVC
controller, allowing centralized exception handling.
SUMATHI R 3
8. Stereotype Annotations?
In Spring, stereotype annotations are a set of annotations used to indicate the role or
purpose of a class within the Spring application context. These annotations help
Spring to understand how the class should be treated and managed by the Spring IoC
container. There are several stereotype annotations in Spring:
@Service`: Indicates that the class is a service or business logic component in the
application. It encapsulates business logic and performs specific tasks.
@Configuration`: Indicates that the class is a configuration class for the Spring
application context. It typically contains bean definitions and configuration settings
using annotations such as `@Bean`, `@ComponentScan`, etc.
SUMATHI R 4
10. Dependency Injection?
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final MyRepository repository;
@Autowired
public MyService(MyRepository repository) {
this.repository = repository;
} // Other methods using repository
}
In Spring Boot, a bean is simply an object that is managed by the Spring framework's
Inversion of Control (IoC) container. The IoC container, also known as the Spring
container, is responsible for instantiating, configuring, and managing these beans
throughout the lifecycle of a Spring application.
Beans in Spring Boot are typically Java objects annotated with Spring's stereotype
annotations such as `@Component`, `@Service`, `@Repository`, or `@Controller`,
among others. These annotations allow Spring to automatically detect and register
beans during application startup.
SUMATHI R 5
Beans can represent various components of the application, including controllers,
services, repositories, configuration classes, etc. By leveraging dependency injection,
Spring Boot wires these beans together, allowing them to collaborate and work
seamlessly within the application context.
Overall, beans play a central role in Spring Boot applications, providing a flexible and
modular way to structure and manage application components.
12. What is the role of the application.properties or application.yml file in a Spring Boot
application?
13. What are Spring Boot profiles, and how are they used? Can you explain how to define
and activate profiles in a Spring Boot application?
Spring Boot profiles allow developers to customize application behaviour for different
environments, such as development, testing, and production. Profiles are activated
using environment-specific properties or command-line arguments.
//application.properties file
//Spring.profiles.active = development
14. How does Spring Boot support externalized configuration? What are the different
sources of externalized configuration in Spring Boot?
SUMATHI R 6
15. How can you override default Spring Boot configurations? What is the purpose of the
`@ConfigurationProperties` annotation, and how is it used in Spring Boot?
To override default Spring Boot configurations, you can provide custom configuration
properties in `application.properties` or `application.yml` files. These properties will
take precedence over the default values defined by Spring Boot."
16. What are Spring Boot starters, and how do they simplify project setup?
Spring Boot starters are pre-configured sets of dependencies that simplify project
setup by providing a cohesive set of libraries for common tasks such as web
development, database access, security, etc. They eliminate the need for manual
dependency management and reduce the complexity of configuring Spring
applications.
Spring Boot applications can be deployed in various ways, including standalone JAR
files, WAR files deployed to servlet containers like Tomcat or Jetty, or as Docker
containers. The choice of deployment method depends on factors such as application
requirements, scalability, and infrastructure setup.
18. What is Spring Boot Actuator and what functionality does it provide?
Spring Boot Actuator is not compulsory, but it's highly recommended for production-
grade applications. It provides essential features for monitoring and managing your
Spring Boot application in production environments. While you're not required to use
it, integrating Spring Boot Actuator can greatly enhance your ability to monitor
application health, gather metrics, and troubleshoot issues.
SUMATHI R 7
19. How does Spring Boot support security in web applications? Can you explain the
various authentication and authorization mechanisms provided by Spring Security in
Spring Boot?
If your application deals with sensitive data, user authentication, or access control,
then incorporating Spring Boot Security becomes essential to ensure proper
protection against unauthorized access and security threats. However, for simple
projects without security requirements, you may choose not to include Spring Boot
Security.
20. What is Spring Data JPA, and how does it simplify data access in Spring Boot
applications? Can you demonstrate how to define and use repositories in Spring Data
JPA?
Spring Data JPA is a part of the larger Spring Data project that simplifies data access in
Spring applications, particularly when working with relational databases. It provides
repository support for CRUD operations, query methods, pagination, and more, with
minimal boilerplate code."
Spring Data JPA works with relational databases like Oracle by providing layer of
abstraction and convenience on top of JPA, which itself is a standard Java API for
accessing, persisting and managing data in relational databases.
Swagger is incredibly useful for drawing blueprints or documenting APIs during the
design phase. It allows developers to define the structure, endpoints,
request/response formats, and even authentication mechanisms in a standardized
format. This documentation serves as a blueprint that can be shared with team
members, stakeholders, and even external consumers of the API. It ensures
consistency, clarity, and ease of understanding, making the API design process more
efficient and collaborative. Additionally, Swagger's interactive documentation feature,
such as Swagger UI, provides a user-friendly interface for exploring and testing APIs,
further aiding in the design and validation process.
SUMATHI R 8
22. How does Swagger benefit API development in Spring Boot?
Swagger can be integrated with a Spring Boot application using the Springfox library,
which provides annotations and configuration classes to generate Swagger
documentation from Spring MVC controllers. By adding Springfox dependencies and
configuring Docket beans, Swagger UI is automatically generated.
24. What are some common annotations used with Swagger in Spring Boot?
Common annotations used with Swagger in Spring Boot include `@Api` to annotate
controller classes, `@ApiOperation` to annotate API operations, `@ApiParam` to
describe method parameters, `@ApiResponse` to define response messages, and
`@ApiModel` to annotate model classes."
25. How can you customize Swagger documentation in a Spring Boot application?
Yes, Swagger can be used for API testing by leveraging its interactive documentation
feature, Swagger UI. Developers and testers can directly interact with API endpoints,
submit requests, and view responses using the Swagger UI interface, making it a
valuable tool for manual and exploratory testing.
SUMATHI R 9
27. What are the different testing techniques available in Spring Boot? How do you write
unit tests and integration tests for Spring Boot applications?
Spring Boot supports various testing techniques, including unit testing with JUnit and
Mockito, integration testing with Spring Boot's testing annotations and embedded
databases, and end-to-end testing with tools like Selenium or REST Assured.
28. How does Spring Boot integrate with Spring Cloud for building distributed systems?
Can you explain some of the key components of Spring Cloud and how they are used
with Spring Boot?
Spring Boot seamlessly integrates with Spring Cloud to build distributed systems.
Spring Cloud provides tools and libraries for common distributed system patterns
such as service discovery (Netflix Eureka), distributed configuration (Spring Cloud
Config), intelligent routing (Netflix Zuul), and more.
29. How can you monitor and manage a Spring Boot application's performance? What
logging frameworks does Spring Boot support, and how can you configure logging
levels and appenders?
Spring Boot applications can be monitored using built-in features like Spring Boot
Actuator endpoints for health checks and metrics. For logging, Spring Boot supports
various logging frameworks such as Logback, Log4j2, and JUL, with configurable
logging levels, appenders, and log formats.
30. What is Spring Boot DevTools, and how do they enhance the development
experience? Can you explain some of the features provided by Spring Boot DevTools?
31. How can you perform input validation in Spring Boot? What is the purpose of the
`@Validated` annotation, and how is it used for method-level validation?
SUMATHI R 10
Input validation in Spring Boot can be performed using Bean Validation API
annotations such as `@NotNull`, `@NotBlank`, `@Size`, etc., along with the `@Valid`
annotation to trigger validation."
32. What are some commonly used endpoints provided by Spring Boot Actuator? How
can you customize and secure Spring Boot Actuator endpoints?
Commonly used endpoints provided by Spring Boot Actuator include `/health`, `/info`,
`/metrics`, `/env`, `/beans`, `/mappings`, etc."
33. How does Spring Boot support caching, and what caching providers does it integrate
with? How can you configure caching in a Spring Boot application?
Spring Boot supports caching through integration with caching providers like Ehcache,
Redis, Caffeine, etc. You can enable caching by adding appropriate dependencies and
annotating methods with `@Cacheable`, `@CacheEvict`, etc."
34. How can you implement messaging in Spring Boot applications using technologies like
RabbitMQ or Apache Kafka? What is the role of Spring Integration in Spring Boot
messaging?
SUMATHI R 11
35. How can you implement internationalization and localization in a Spring Boot
application? What are some common strategies for managing localized messages and
resources in Spring Boot?
"Common strategies for managing localized messages and resources in Spring Boot
include using message bundles for different languages, organizing message files based
on locales, and supporting dynamic locale changes based on user preferences."
The Spring container is responsible for managing the components (beans) of a Spring
application, including their instantiation, configuration, and lifecycle management. It
achieves this through the concept of Inversion of Control (IoC), where the control
over object instantiation and management is inverted from the application code to
the container.
So, when we talk about the Spring container or IoC container, we are referring to the
same thing: the core mechanism provided by the Spring Framework for managing
beans and their dependencies. This container is responsible for implementing the
principles of IoC, such as dependency injection and loose coupling, which are central
to the design philosophy of the Spring Framework.
"components" and "beans" are closely related but not the same.
SUMATHI R 12
annotated with stereotype annotations or beans defined explicitly in configuration
classes using `@Bean`.
So, while all beans are components, not all components are beans. Components are
classes that are managed by the Spring container, and beans are instances of those
classes that are managed by the container. The term "bean" is often used more
specifically to refer to instances of classes managed by the container.
"Spring MVC is a web framework built on top of the Spring Framework, designed to
simplify the development of web applications in Java. It follows the Model-View-
Controller (MVC) architectural pattern, which separates the application into three
main components:
1. **Model**: Represents the application's data and business logic. In Spring MVC,
model objects are typically Java objects (POJOs) that encapsulate data and behavior
relevant to the application.
2. **View**: Renders the user interface and presents data to the user. Views in
Spring MVC are often implemented using technologies like JSP (JavaServer Pages),
Thymeleaf, or FreeMarker.
3. **Controller**: Handles user requests, processes input, and coordinates with the
model and view components. Controllers in Spring MVC are implemented as Java
classes annotated with `@Controller`, which handle specific URL mappings and HTTP
methods.
Overall, Spring MVC offers a flexible and powerful framework for building web applications in
Java, leveraging the features and capabilities of the underlying Spring Framework to simplify
development, improve maintainability, and enhance scalability."
SUMATHI R 13