Spring Boot Interview Q_A for (2-3 Yrs of Exp)
Spring Boot Interview Q_A for (2-3 Yrs of Exp)
A: Simplify Spring app setup with auto-configuration, embedded servers, and opinionated defaults.
A: Servers like Tomcat/Jetty bundled within the app, so no external deployment is needed.
6. What is spring-boot-devtools?
A: A module for automatic app restarts and live reload during development.
8. What is CommandLineRunner?
A: An interface to execute code after the app starts (e.g., run() method).
A: Used in Spring Cloud Config to load external configuration before the main app starts.
8. What is ResponseEntity?
A: CrudRepository = basic CRUD. JpaRepository adds JPA-specific features (e.g., flushing, batch ops).
A: Too many queries due to lazy loading. Fix with JOIN FETCH in JPQL or @EntityGraph.
8. What is PagingAndSortingRepository?
A: Use Spring Security by adding the spring-boot-starter-security dependency, configuring HTTP security,
and defining security rules.
A: @PreAuthorize uses SpEL for method-level security, while @Secured is a simpler, role-based
annotation.
A: By default, Spring Security enables CSRF protection. To disable it, configure HttpSecurity with
.csrf().disable().
Microservices (Spring Cloud) (7 Questions)
1. What is Spring Cloud?
A: A framework providing tools for building distributed systems and microservices (e.g., service
discovery, load balancing, config server).
A: A service discovery tool for registering and locating microservices. Eureka Server acts as a registry,
and clients use Eureka Client to discover services.
A: A single entry point for routing requests to various services. Implemented using Spring Cloud Gateway
or Zuul.
A: A resilience pattern preventing cascading failures by stopping requests to unresponsive services (e.g.,
Hystrix, Resilience4j).
A: Use Spring Cloud Config Server to externalize configuration files for all microservices.
A: A declarative HTTP client for making REST calls between microservices, simplifying inter-service
communication.
A: Sleuth provides distributed tracing, while Zipkin is a tool for visualizing trace data and diagnosing
latency issues.
A: JUnit 5 introduced modular architecture, better extension support, and annotations like @BeforeAll,
@AfterAll.
A: Use @Mock annotation or Mockito.mock() method and Mockito.when() to define mock behavior.
A: Use MockMvc for unit testing controllers without starting the server.
4. What is the purpose of @MockBean in Spring Boot tests?
A: Use @DataJpaTest annotation to test only JPA components without starting the full context.
A: Disable unused auto-configurations, use lazy initialization, and optimize bean creation.
A: Use SLF4J with Logback, apply logging levels properly, and utilize MDC for traceability.
4. How to improve query performance with JPA? A: Use projections, DTOs, pagination, and batch
fetching.