Spring Basics, Spring Boot and Microservices Interview Questions

  1. What is Spring Boot?

    Spring Boot is a Java-based Spring framework that makes it easy to develop stand-alone (build microservices) applications. Most Spring Boot applications require minimal configuration due to its auto-configuration and embedded application server support like Tomcat, Jetty, etc.

  2. How does Spring Boot differ from Spring?

    Spring Boot differs from Spring in several ways:

    • Configuration: Spring requires manual configuration, whereas Spring Boot uses auto-configuration.
    • Server: Spring requires an external server/container, while Spring Boot includes a stand-alone application with an embedded server.
    • Database: Spring typically requires an external database, while Spring Boot can use an in-memory database.
  3. What are the advantages of Spring Boot?

    The advantages of Spring Boot include:

    • Minimal configuration required.
    • Easy to understand and develop Spring applications.
    • Increases productivity and reduces development time.
    • Provides an easy way to create Spring-based applications using Java or Groovy.
    • Avoids complex XML configurations with minimal annotations.
  4. What are the features of Spring Boot?

    The features of Spring Boot include:

    • Create stand-alone Spring applications.
    • Provide opinionated 'starter' dependencies to simplify build configuration.
    • No need for a WAR file to deploy applications into an embedded Tomcat, Jetty.
    • Automatically configure Spring and 3rd party libraries.
    • Production-ready features like metrics, health checks, and externalized configuration.
    • No code generation and no requirement for XML configuration.
  5. What are the key components of Spring Boot?

    The key components of Spring Boot are:

    • Auto-configuration
    • Starter POMs
    • CLI
    • Actuators
  6. What are some important Spring Boot annotations?

    Some important Spring Boot annotations are:

    • @Conditional
    • @EnableAutoConfiguration
    • @SpringBootApplication
    • @AutoConfiguration
    • @EnableCaching
  7. What is Spring Initializr?

    Spring Initializr is used to create new or skeleton Spring Boot projects.

    Steps to create a Spring project using Spring Initializr:

    • Go to Spring Initializr.
    • Choose dependency management tool (Maven/Gradle), language (Java, Kotlin, Groovy), packaging (Jar/War), version, and dependencies.
    • Generate and download the project.
    • Unzip and import into your IDE (e.g., Eclipse).
  8. What are the properties of Spring Boot?

    Spring Boot provides various properties that can be configured in the application.properties file, such as spring.datasource.url, server.port, spring.profiles.active, etc.

  9. How to disable specific auto-configuration?

    Use @EnableAutoConfiguration(exclude = .class) to disable specific auto-configuration.

    Example:

    @EnableAutoConfiguration(exclude = SpringTestApplication.class)
  10. How does Spring Boot work?

    Spring Boot works through the automatic configuration of your application based on dependencies. It primarily uses the @SpringBootApplication annotation and a main method to run the application.

  11. What is Thymeleaf?

    Thymeleaf is a popular templating engine used in Spring Boot applications to build dynamic web pages.

  12. What are some commonly used Spring Boot starters?

    Commonly used Spring Boot starters include:

    • spring-boot-starter: core starter with auto-configuration support
    • spring-boot-starter-aop: for aspect-oriented programming
    • spring-boot-starter-data-jpa: for using Spring Data JPA with Hibernate
    • spring-boot-starter-security: for using Spring Security
    • spring-boot-starter-test: for testing Spring Boot applications
    • spring-boot-starter-web: for building web applications using Spring MVC
  13. How to use Thymeleaf?

    Add the thymeleaf-spring-boot-starter dependency in your pom.xml file.

    Example:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  14. How does the @SpringBootApplication annotation work internally?

    @SpringBootApplication is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan.

  15. What is the @RestController annotation?

    @RestController is a combination of @Controller and @ResponseBody, used to create RESTful web services. It converts the response to JSON or XML.

  16. What are the key dependencies of Spring Boot?

    The key dependencies of Spring Boot include:

    • spring-boot-maven-plugin
    • spring-boot-starter-test
    • spring-boot-starter-parent
    • spring-boot-starter-web
    • spring-boot-starter-security
    • spring-boot-starter-actuator
  17. What is the difference between @RestController and @Controller?

    @Controller maps the model object to a view or template, making it readable. @RestController simply returns the object, with the object data directly written in the HTTP response as JSON or XML.

  18. What is Spring Actuator?

    Spring Actuator provides additional features for monitoring and managing your application in production, such as metrics, health checks, and configuration properties.

  19. How to enable Actuator in a Spring Boot application?

    Add the spring-boot-starter-actuator dependency in your pom.xml file.

    Example:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  20. How to change the default port in Spring Boot?

    Change the default port by specifying the server.port property in the application.properties file.

    Example:

    server.port=8081
  21. What is the use of Spring Boot DevTools?

    Spring Boot DevTools provides additional development-time features like automatic restarts, live reload, and configurations for faster application development.

  22. How to enable DevTools in Spring Boot?

    Add the spring-boot-devtools dependency in your pom.xml file.

    Example:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
  23. How to enable scheduling in Spring Boot?

    Enable scheduling by adding the @EnableScheduling annotation to a configuration class.

    Example:

    @Configuration
    @EnableScheduling
    public class SchedulingConfig {
    }
  24. What is Spring Cloud?

    Spring Cloud provides tools for developers to quickly build distributed systems with common patterns such as configuration management, service discovery, circuit breakers, routing, etc.

  25. What is microservices architecture?

    Microservices architecture is a design style that structures an application as a collection of small, loosely coupled services that can be developed, deployed, and scaled independently.

  26. What are the advantages of microservices?

    The advantages of microservices include:

    • Scalability
    • Independent deployment
    • Flexibility in technology stack
    • Fault isolation
    • Ease of understanding
    • Continuous delivery
  27. What is a distributed system?

    A distributed system is a network that consists of multiple autonomous computers that communicate and coordinate with each other to achieve a common goal.

  28. What is the role of Spring Boot in microservices?

    Spring Boot simplifies the creation of production-ready applications and provides a wide range of tools for building and deploying microservices.

  29. What is Netflix Eureka?

    Netflix Eureka is a service registry that allows microservices to register themselves and discover other registered services.

  30. What is the role of Zuul in microservices?

    Zuul is an edge service that provides dynamic routing, monitoring, resiliency, and security for microservices.

  31. What is load balancing?

    Load balancing is a way to distribute incoming network traffic across multiple servers, ensuring that no single server becomes overloaded.

  32. How to enable Eureka Client?

    Add spring-cloud-starter-netflix-eureka-client dependency in your pom.xml and configure application properties.

  33. How does communication happen between microservices?

    Communication between microservices can happen via:

    • Synchronous communication via HTTP/REST
    • Asynchronous communication via message brokers like RabbitMQ, Kafka
  34. What is Spring Cloud Config?

    Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system.

  35. How to implement service discovery?

    Service discovery can be implemented using Netflix Eureka in Spring Cloud.

  36. What is Hystrix?

    Hystrix is a latency and fault-tolerance library designed to isolate access points to remote systems and services, preventing cascading failures.

  37. What are API Gateways?

    API Gateways handle routing requests, composition, and providing protocol translations. They act as an entry point for client requests.

  38. What are Feign Clients?

    Feign is a declarative web service client, making it easier to write web service clients by simplifying HTTP API calls using annotations.

  39. What is the use of Zuul?

    Zuul is an edge server that provides dynamic routing, monitoring, security, and resilience in microservices architecture.

  40. What is service registry?

    A service registry is a database of services and their locations, which enables dynamic discovery and connection of services.

  41. What is the use of @EnableDiscoveryClient?

    @EnableDiscoveryClient is used to register a service with a discovery service like Eureka.

  42. What is Ribbon?

    Ribbon is a client-side load balancer that allows for configuring a list of servers to distribute the load.

  43. What is Circuit Breaker in microservices?

    A circuit breaker pattern is a design pattern used to detect failures and encapsulate the logic of preventing the failure from constantly recurring.

  44. How to implement the circuit breaker pattern?

    Implement circuit breaker pattern using Hystrix with annotations like @HystrixCommand.

  45. What is the role of DevOps in microservices?

    DevOps ensures continuous integration and deployment, making it easier to manage the microservices lifecycle.

  46. What are the common challenges with microservices?

    Common challenges with microservices include:

    • Distributed logging and tracing
    • Testing complexity
    • Data consistency
    • Deployment complexity
    • Service discovery and load balancing
  47. What are the best practices for designing microservices?

    Best practices for designing microservices include:

    • Define clear service boundaries
    • Use APIs for communication
    • Decentralize data management
    • Automate deployment and monitoring
    • Implement resilience patterns
    • Ensure security at every level