https://github.com/c-akhil/spring-boot-interview-guide
A comprehensive guide to prepare for Spring Boot interviews. Covers core concepts, dependency injection, microservices, testing, and DevOps best practices.
https://github.com/c-akhil/spring-boot-interview-guide
backend interview-prep java java-8 microservices spring-boot
Last synced: 8 months ago
JSON representation
A comprehensive guide to prepare for Spring Boot interviews. Covers core concepts, dependency injection, microservices, testing, and DevOps best practices.
- Host: GitHub
- URL: https://github.com/c-akhil/spring-boot-interview-guide
- Owner: c-akhil
- Created: 2025-02-14T08:33:31.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-14T09:12:13.000Z (11 months ago)
- Last Synced: 2025-02-14T09:38:01.654Z (11 months ago)
- Topics: backend, interview-prep, java, java-8, microservices, spring-boot
- Homepage:
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Core Spring Boot Concepts
---
### What is Spring Boot, and how is it different from the Spring Framework?
Spring Boot is an opinionated framework built on top of the Spring Framework. It simplifies development by providing auto-configuration, starter dependencies, and embedded servers so that you can build stand-alone, production-grade applications with minimal setup. In contrast, while the Spring Framework is a powerful and comprehensive toolset for enterprise applications, it requires significant manual configuration and setup.
---
### What are the key features and advantages of Spring Boot?
- Auto-Configuration: Automatically configures Spring beans based on the classpath.
- Starters: Predefined dependency descriptors (e.g., spring-boot-starter-web) that simplify dependency management.
- Embedded Servers: Built-in support for embedded Tomcat, Jetty, or Undertow to run applications without external servers.
- Production-Ready Features: Metrics, health checks, and externalized configuration via Actuator.
- Rapid Development: Reduced boilerplate and a focus on convention over configuration allow developers to quickly build and deploy applications.
---
### What is the role of @SpringBootApplication?
This annotation is a convenience annotation that combines three annotations:
- @Configuration – marks the class as a source of bean definitions.
- @EnableAutoConfiguration – tells Spring Boot to automatically configure your application based on the dependencies on the classpath.
- @ComponentScan – enables scanning for components in the same package and subpackages.
It serves as the main entry point for Spring Boot applications.
---
### How does Spring Boot handle auto-configuration?
Spring Boot’s auto-configuration mechanism examines the classpath, environment, and defined beans, then automatically configures beans and settings to reduce manual configuration. It uses conditional annotations (e.g., @ConditionalOnClass) to apply configuration only when certain classes or conditions are present, while still allowing you to override defaults.
---
### What are Spring Boot starters, and why are they useful?
Starters are curated dependency bundles that aggregate all necessary libraries for a particular functionality (e.g., web development, data access, security). They simplify dependency management by reducing the need to specify multiple dependencies individually, ensuring that compatible versions are used together.
---
## Dependency Injection & Bean Management
### What is Dependency Injection, and what are its types in Spring?
Dependency Injection (DI) is a design pattern in which an object’s dependencies are provided (or “injected”) by an external entity rather than created by the object itself. In Spring, DI is implemented mainly as:
- Constructor Injection: Dependencies are provided through a class constructor.
- Setter Injection: Dependencies are assigned via setter methods.
- Field Injection: Dependencies are injected directly into fields (using @Autowired), though this is less favored for testability and immutability reasons.
---
### What is the difference between @Component, @Service, and @Repository?
All three annotations mark a class as a Spring-managed bean but signal different roles:
- @Component: A generic stereotype for any Spring-managed component.
- @Service: Indicates a service-layer class; it doesn’t add extra behavior but clarifies intent.
- @Repository: Used for data access layers; it provides exception translation from persistence-specific exceptions to Spring’s data access exceptions.
---
### How does Spring Boot manage the bean lifecycle?
Spring Boot (via the Spring container) instantiates beans, injects their dependencies, and applies lifecycle callbacks. Beans can implement lifecycle interfaces (e.g., InitializingBean and DisposableBean) or use annotations like @PostConstruct and @PreDestroy to perform actions upon initialization and before destruction. Bean post-processors can further modify beans before and after initialization.
---
### What is the difference between @Autowired, @Qualifier, and @Primary?
- @Autowired: Injects a bean by type automatically.
- @Qualifier: Used alongside @Autowired when multiple beans of the same type exist; it specifies which bean should be injected based on a qualifier or bean name.
- @Primary: Marks a bean as the default choice for injection when multiple candidates exist.
---
### How do you define bean scopes in Spring Boot?
Bean scopes can be defined using the @Scope annotation. The most common scopes are:
- Singleton (default): One instance per Spring container.
- Prototype: A new instance each time a bean is requested.
- Request: One instance per HTTP request (used in web applications).
- Session: One instance per HTTP session.
Additional scopes (like application or websocket) are also available for specific contexts.
Configuration & Properties Management
---
### How do you configure application properties in Spring Boot?
Configuration is typically done via external configuration files such as application.properties or application.yml. These files allow you to define values (e.g., server port, database URL) that Spring Boot uses to configure beans and auto-configuration settings at startup.
---
### What is the difference between application.properties and application.yml?
- application.properties: Uses a simple key-value pair format (key=value).
- application.yml: Uses YAML syntax, which supports hierarchical structures and is often more readable when dealing with nested configurations.
---
### How do you manage multiple environments (dev, prod, test) in Spring Boot?
Multiple environments can be managed using Spring Profiles. You can create profile-specific configuration files (e.g., application-dev.properties, application-prod.properties) and activate the desired profile using the spring.profiles.active property (set via command-line, environment variables, or within the configuration files).
---
### What is the purpose of @Value, and how is it used?
The @Value annotation is used to inject values from external configuration files into Spring-managed beans. For example, @Value("${app.name}") injects the value of the property app.name defined in your configuration files.
---
### How do you externalize configuration for microservices?
External configuration for microservices is often managed using Spring Cloud Config, which provides a centralized configuration server. This server serves configuration properties from a repository (e.g., Git), allowing services to fetch their configuration dynamically at startup or even refresh them at runtime.
Spring Boot REST API Development
---
### What is the difference between @Controller and @RestController?
- @Controller: Marks a class as a web controller that returns view names, typically used in MVC applications.
- @RestController: A specialized version of @Controller that automatically applies @ResponseBody to all methods, meaning that responses are written directly to the HTTP response body (often as JSON or XML) and no view resolution is performed.
---
### What are @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping used for?
These annotations map HTTP methods to controller methods:
- @GetMapping handles GET requests.
- @PostMapping handles POST requests.
- @PutMapping handles PUT requests.
- @DeleteMapping handles DELETE requests.
They provide clear, method-specific mappings compared to the more generic @RequestMapping.
---
### How do you handle exceptions in a Spring Boot REST API?
Exception handling is commonly managed using @ControllerAdvice combined with @ExceptionHandler methods, which provide centralized handling of exceptions across controllers. Spring Boot also includes a default error handling mechanism (via BasicErrorController) that returns a standardized error response.
---
### What is the difference between @RequestParam and @PathVariable?
- @RequestParam: Extracts query parameters, form data, or request parameters from the URL (e.g., /search?query=spring).
- @PathVariable: Extracts values from the URI path (e.g., /users/{id}), binding the path segment directly to a method parameter.
---
### How do you implement pagination and sorting in Spring Boot?
Spring Data JPA provides the Pageable interface and Page response types to simplify pagination and sorting. By including a Pageable parameter in repository methods or controller endpoints, Spring Data automatically applies pagination and sorting based on query parameters.
---
### How do you secure REST APIs using Spring Security?
REST APIs are secured by configuring Spring Security to enforce authentication and authorization. This can be done using basic authentication, form login, JWT tokens, or OAuth2. Custom security filters can be applied to intercept and validate requests, and role-based access control can be enforced via annotations such as @PreAuthorize.
---
## Database Integration & JPA
### What is Spring Data JPA, and how does it simplify database interactions?
Spring Data JPA is an abstraction layer over the Java Persistence API (JPA) that reduces boilerplate code for database operations. By extending interfaces like JpaRepository, you gain CRUD operations, pagination, and query derivation without having to write explicit DAO implementations.
---
### How do you configure a database connection in Spring Boot?
Database connections are configured in the application’s properties (or YAML) file using properties such as:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
Spring Boot auto-configures a DataSource based on these settings.
---
### What is the difference between JpaRepository and CrudRepository?
- CrudRepository: Provides basic CRUD operations.
- JpaRepository: Extends CrudRepository and adds JPA-specific methods such as pagination, batch operations, and flushing. It is more feature-rich for JPA-based persistence.
---
### What are the different fetching strategies in JPA (EAGER vs. LAZY)?
- EAGER: The associated entities are fetched immediately with the parent entity.
- LAZY: The associated entities are fetched only when accessed, which can improve performance but requires careful handling to avoid lazy initialization exceptions.
---
### How do you handle transactions in Spring Boot (@Transactional annotation)?
The @Transactional annotation demarcates methods or classes that should execute within a transactional context. Spring manages transaction boundaries automatically, committing on success and rolling back on runtime exceptions.
---
### What is HikariCP, and why is it used for database connection pooling?
HikariCP is a high-performance, lightweight JDBC connection pool. It is the default connection pool in Spring Boot due to its efficiency and low overhead, which helps manage database connections reliably in production environments.
---
## Caching & Performance Optimization
### How do you enable caching in Spring Boot?
Caching is enabled by adding the @EnableCaching annotation to a configuration class. Once enabled, you can annotate methods with caching annotations like @Cacheable to cache method results.
---
### What is the difference between @Cacheable, @CachePut, and @CacheEvict?
- @Cacheable: Caches the result of a method call; subsequent calls with the same parameters return the cached value.
- @CachePut: Updates the cache with the method’s result without skipping method execution.
- @CacheEvict: Removes data from the cache, which is useful for invalidating stale entries.
---
### What are the different caching providers supported in Spring Boot?
Spring Boot supports several caching providers including EhCache, Hazelcast, Infinispan, Redis, and a simple in-memory cache based on ConcurrentHashMap.
---
### How do you optimize SQL queries in Spring Boot?
- Analyze and optimize query execution plans.
- Use proper indexing on database tables.
- Avoid N+1 select problems by configuring fetch strategies (e.g., using fetch joins).
- Utilize pagination and limit the amount of data retrieved.
---
### What strategies can improve Spring Boot performance?
- Enable caching to reduce repetitive processing.
- Optimize database queries and use connection pooling (e.g., HikariCP).
- Employ asynchronous processing where appropriate.
- Monitor performance with Actuator, profiling tools, and proper logging.
- Tune auto-configuration and bean creation for resource efficiency.
---
## Spring Boot Security & Authentication
### What is Spring Security, and how does it work?
Spring Security is a robust framework for authentication and authorization. It works by intercepting HTTP requests via security filters, validating credentials, and enforcing access rules. It integrates with the Spring ecosystem to secure web applications and REST APIs against common vulnerabilities.
---
### How do you implement JWT (JSON Web Token) authentication in Spring Boot?
- Create filters that intercept requests and extract JWT tokens.
- Validate tokens using libraries such as JJWT or Spring Security OAuth.
- On successful validation, set the authentication in the security context.
- Configure endpoints to require a valid JWT for access.
---
### What is OAuth2, and how does Spring Boot support it?
OAuth2 is an authorization framework that enables third-party applications to obtain limited access to HTTP services. Spring Boot supports OAuth2 via Spring Security and related modules (e.g., Spring Security OAuth), which simplify the configuration of authorization servers, resource servers, and token management.
---
### How do you prevent security vulnerabilities like CSRF attacks in Spring Boot?
- CSRF protection is enabled by default for web applications in Spring Security.
- For stateless REST APIs (especially when using tokens), CSRF protection may be disabled.
- Always enforce HTTPS, use secure cookies, and validate request origins as additional safeguards.
---
### What are role-based and method-level security implementations?
- Role-Based Security: Access to endpoints or resources is restricted based on user roles.
- Method-Level Security: Using annotations such as @PreAuthorize, @Secured, or @RolesAllowed directly on methods to enforce security at a granular level.
Messaging & Event-Driven Architecture
---
### What is Spring Boot Messaging, and how does it work?
Spring Boot Messaging integrates message brokers (like RabbitMQ, Kafka, or JMS) into your application, allowing for asynchronous communication between components or services. It abstracts many of the lower-level details, enabling you to focus on the business logic of message production and consumption.
---
### How do you integrate RabbitMQ with Spring Boot?
- Include the spring-boot-starter-amqp dependency.
- Configure RabbitMQ connection properties (host, port, credentials) in your properties file.
- Use RabbitTemplate to send messages and annotate listener methods with @RabbitListener to consume messages.
---
### How do you implement event-driven communication using Apache Kafka?
- Include the spring-kafka dependency.
- Configure Kafka producer and consumer properties in your application configuration.
- Use @KafkaListener on methods to consume messages from Kafka topics, and use KafkaTemplate to produce messages.
---
### What is the purpose of @EventListener in Spring Boot?
The @EventListener annotation marks methods as listeners for application events. It decouples event publication from handling, allowing different parts of your application to react to events asynchronously without direct dependencies.
---
### What is the difference between synchronous and asynchronous messaging?
- Synchronous Messaging: The sender waits for the receiver to process the message and return a response, potentially blocking execution.
- Asynchronous Messaging: The sender dispatches a message and continues execution immediately, with the receiver processing the message independently, which can improve responsiveness and scalability.
---
## Microservices & Cloud Integration
### What are microservices, and why is Spring Boot a good fit for them?
Microservices are an architectural style in which an application is composed of small, independent services that communicate over a network. Spring Boot’s lightweight, stand-alone nature combined with its embedded server support and easy integration with Spring Cloud makes it an excellent fit for developing microservices.
---
### What is Spring Cloud, and how does it help in microservices?
Spring Cloud provides tools to address common challenges in microservices, such as externalized configuration, service discovery, load balancing, circuit breakers, and distributed tracing. It works seamlessly with Spring Boot to enable the development of scalable and resilient microservices.
---
### How does Eureka Server enable service discovery?
Eureka Server acts as a registry where microservices register themselves. Other services can query Eureka to locate service instances dynamically, enabling client-side load balancing and fault tolerance.
---
### What is API Gateway, and how does it work in microservices?
An API Gateway is a single entry point for all client requests to a suite of microservices. It handles routing, security, rate limiting, and sometimes response aggregation. Spring Cloud Gateway is one such tool that simplifies this pattern in the Spring ecosystem.
---
### What is Circuit Breaker, and how is it implemented in Spring Boot?
A circuit breaker prevents a service from repeatedly calling a failing remote service, thereby avoiding cascading failures. In Spring Boot, libraries like Resilience4j (and formerly Hystrix) are used to implement circuit breakers that monitor remote calls and “open” the circuit when a threshold of failures is reached.
---
### How do you handle centralized configuration using Spring Cloud Config?
Spring Cloud Config provides a centralized server that serves configuration data (from sources like Git or SVN) to all microservices. Clients connect to the Config Server at startup (or refresh on demand) to retrieve their configuration, ensuring consistency across environments.
---
### How does Spring Boot support distributed tracing in microservices?
Spring Boot integrates with Spring Cloud Sleuth and Zipkin, which automatically add trace and span IDs to logs and HTTP headers. This enables tracking a request’s journey across multiple microservices, simplifying debugging and performance monitoring.
---
## Testing Best Practices
### How do you write unit tests in Spring Boot?
Unit tests are written using frameworks like JUnit (or TestNG) along with Spring’s testing support. You can annotate your test classes with @ExtendWith(SpringExtension.class) (for JUnit 5) and use mocking frameworks like Mockito. It’s best to keep unit tests isolated from external dependencies.
---
### What is @MockBean, and how does it help in testing?
@MockBean is used in Spring Boot tests to create and inject a mock of a bean into the application context. This allows you to isolate the component under test by replacing its dependencies with mocks.
---
### What is the difference between @SpringBootTest and @WebMvcTest?
- @SpringBootTest loads the entire Spring application context and is used for integration testing.
- @WebMvcTest focuses only on the web layer (controllers, filters, etc.) and loads a subset of configurations, making tests faster and more focused.
---
### How do you perform integration testing in Spring Boot?
Integration tests load the full application context (using @SpringBootTest) and test interactions between components, including controllers, services, and repositories. Tools like TestRestTemplate or MockMvc help simulate HTTP requests and verify the end-to-end behavior of your application.
---
### What are best practices for writing test cases in Spring Boot?
- Keep tests small, focused, and independent.
- Use proper annotations to load only what is needed (e.g., @WebMvcTest for controller tests).
- Replace external dependencies with mocks using @MockBean or similar tools.
- Maintain clear naming conventions and organize tests according to functionality.
- Integrate tests into your CI/CD pipeline to ensure they run on every build.
---
## Deployment & DevOps
### How do you package a Spring Boot application for deployment?
Spring Boot applications are typically packaged as executable JAR files using build tools like Maven or Gradle. These JARs include an embedded server, making deployment simple. Alternatively, you can package them as WAR files if you need to deploy to an external servlet container.
---
### What is the difference between JAR and WAR deployment in Spring Boot?
- JAR Deployment: Packages the application along with an embedded server, allowing it to run independently.
- WAR Deployment: Packages the application for deployment to an external servlet container (e.g., Tomcat), which can be beneficial in traditional enterprise environments.
---
### How do you deploy a Spring Boot application using Docker?
1. Create a Dockerfile that uses a base image (e.g., OpenJDK).
2. Copy your executable JAR into the container.
3. Define the container’s entry point (typically using CMD to run the JAR).
4. Build the Docker image and run it as a container. Docker Compose can manage multi-container setups if needed.
---
### How does Spring Boot integrate with Kubernetes for container orchestration?
Once containerized, Spring Boot applications can be deployed on Kubernetes, which manages scaling, service discovery, and load balancing. Spring Boot Actuator endpoints (for health and metrics) can be used by Kubernetes liveness and readiness probes. Helm charts and Kubernetes manifests simplify deployment and configuration.
---
### What is CI/CD, and how do you implement it for Spring Boot applications?
- CI/CD (Continuous Integration/Continuous Deployment): Practices that automate the build, testing, and deployment of applications.
- For Spring Boot, CI/CD pipelines can be set up using tools like Jenkins, GitLab CI/CD, or GitHub Actions to automatically build the JAR/WAR, run tests, and deploy to environments (or container registries).
---
### What are the best logging and monitoring tools for Spring Boot?
- Logging: Spring Boot supports Logback (default), Log4j2, and Java Util Logging.
- Monitoring: Spring Boot Actuator provides endpoints for health, metrics, and environment info. Other tools include Prometheus/Grafana for metrics, the ELK stack (Elasticsearch, Logstash, Kibana) for log aggregation, and Zipkin for distributed tracing.