Spring Boot Interview Questions – 1

Q1. What is the use of @SpringBootApplication?

It’s a convenience annotation that combines:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

Q2. What is the purpose of the application.properties or application.yml file?

  • In Spring Boot, the application.properties or application.yml file serves as the centralized configuration source for the application.
  • We often externalize these configs to cloud-native config servers (like Spring Cloud Config Server or AWS Parameter Store) for dynamic and secure management in microservices.
  • In containerized environments (e.g., Kubernetes), we inject environment-specific values using ConfigMaps and Secrets, overriding values in these files via environment variables or command-line args.

Q3. How do you enable or disable specific auto-configurations in Spring Boot?

Q4. What are @Qualifier and @Primary used for?

  • @Qualifier resolves ambiguity when multiple beans of the same type exist, allowing you to specify which bean to inject.
  • @Primary is used to mark a bean as the default choice when multiple beans of the same type are available, and no specific @Qualifier is provided.

Q5. What is an Actuator in Spring Boot?

  • Spring Boot Actuator is a module that provides production-ready features to monitor and manage your Spring Boot application. It offers various endpoints and metrics that can be used for monitoring, health checks, auditing, and managing your application.
  • By default, Actuator endpoints are enabled, but we can customize the configuration in the application.properties or application.yml file.

Q6. How do you handle exceptions in Spring Boot?

  • Use @RestControllerAdvice to handle application-wide exceptions in one place.
  • Create domain-specific exceptions like ProductNotFoundException, etc., to express intent clearly and separate business concerns.
  • Define reusable ErrorResponse records with fields like timestamp, status, message, path, and correlationId. Basically, a DTO response.
  • To enable end-to-end tracing, use correlation/request IDs (from headers or MDC) in logs and error responses.

Q7. What is the difference between @Component, @Service, and @Repository annotations?

  • @Component: It is a generic component like a utility class, helper class, etc.
  • @Service: indicates a service layer for business logic, transactional logic like Read-Write-Update, etc.
  • @Repository: Marks a DAO class and helps with exception translation, DataAccessException.

Q8. What are HTTP methods in springBoot? What are their use?

Mapping annotation in Spring simply informs that this method handles HTTP requests as per their respective verb, like GET, POST, PATCH, PUT, etc.

  • @PatchMapping It doesn’t define how partial updates should be applied — that logic is up to you.
  • @PutMapping It is used to completely replace an existing resource.
  • @GetMapping It is used to retrieve resources or data, without modifying the server state.
  • @PostMapping It is primarily used to create new resources.
Read more for more information

Leave a Reply

Your email address will not be published. Required fields are marked *