Spring - Spring Boot Flashcards

1
Q

Spring Boot offers several advantages over traditional Spring Framework setups?

A
  • Spring Boot is an extension of the Spring Framework that simplifies the setup, development, and deployment of Spring applications.
  • It removes much of the boilerplate configuration needed in traditional Spring applications.

Key differences from traditional Spring:
* Auto-configuration: Spring Boot automatically configures beans based on classpath settings, avoiding manual setup.
* Embedded Servers: It comes with embedded Tomcat, Jetty, or Undertow, so we don’t need to deploy WAR files to an external server.
* Starter Dependencies: It provides starter POMs to simplify dependency management (e.g., spring-boot-starter-web).
* Production-Ready Features: Built-in health checks, metrics, and externalized configuration via Actuator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain what Spring Boot Starters are

A
  • Spring Boot Starters are a set of dependency descriptors provided by Spring Boot to** simplify Maven/Gradle configuration**.
  • Each starter is a pre-defined set of dependencies for a particular functionality or technology stack.

Examples:
* spring-boot-starter-web: includes Spring MVC, Jackson (for JSON), embedded Tomcat, and more.
* spring-boot-starter-data-jpa: includes Spring Data JPA, Hibernate, and a database connection pool.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Spring Boot’s auto-configuration mechanism?

A
  • Auto-configuration in Spring Boot attempts to automatically configure your application based on the dependencies present on the classpath and configuration properties.
  • It is enabled by the @SpringBootApplication annotation, which implicitly includes @EnableAutoConfiguration.
  • Each auto-configuration class is typically annotated with @Conditional annotations like:
    @ConditionalOnClass — only configure if a specific class is on the classpath.
    @ConditionalOnMissingBean — only configure if a bean is not already defined.
    @ConditionalOnProperty — only configure if a specific property is present.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the role of the @SpringBootApplication annotation, and what does it encapsulate?

A

The @SpringBootApplication annotation is a convenience annotation that combines several commonly used Spring annotations:

@Configuration – Marks the class as a source of bean definitions.

@EnableAutoConfiguration – Enables Spring Boot’s auto-configuration mechanism, which attempts to automatically configure your application based on the dependencies present.

@ComponentScan – Enables component scanning so that Spring can find and register beans (like @Component, @Service, @Repository, etc.) in the same package or sub-packages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does Spring Boot handle externalized configuration, and what are the common ways to define and load application settings?

A

Common ways to define and load configuration:

  • application.properties or application.yml: The most common way. Located in src/main/resources or externalized via command line or environment.
  • Command-line arguments: You can pass properties like --server.port=8081 when starting the app.
  • Environment variables: Useful in containerized or cloud environments (e.g., SERVER_PORT=8081).
  • System properties: Passed via JVM args (e.g., -Dserver.port=8081).
  • Profile-specific files: Files like application-dev.yml, application-prod.yml tied to active profiles.
  • @Value annotation: Injects specific property values into beans.
  • @ConfigurationProperties: Binds groups of related properties into a POJO.

Spring Boot loads these sources in a specific order of precedence, where command-line arguments override everything, followed by environment variables, then properties/yml files, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Spring Profiles in Spring Boot?

A

Spring Profiles in Spring Boot allow you to define and group configuration and bean definitions based on the environment (e.g., development, testing, production). They help manage environment-specific behavior without changing the code.

Key aspects:

  • Activating profiles: Use the spring.profiles.active property in application.properties, command-line args, or environment variables (e.g., --spring.profiles.active=dev).
  • Profile-specific configuration files: Use files like application-dev.yml, application-prod.yml, etc., which are only loaded when the corresponding profile is active.
  • Profile-based beans: Use @Profile("dev") or similar annotations on beans/classes to activate them only in certain profiles.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Spring Boot Actuator, and what kind of features does it provide for production-ready applications?

A

Spring Boot Actuator is a submodule of Spring Boot that provides built-in production-ready features to help monitor and manage applications.

Key features include:

  • Endpoints: Exposes various HTTP endpoints for operational information (e.g., /actuator/health, /actuator/metrics, /actuator/info, etc.).
  • Health checks: Shows application health status, including DB connectivity, disk space, and custom checks.
  • Metrics: Provides detailed metrics like memory usage, CPU, garbage collection, and custom application metrics.
  • Auditing and tracing: Helps trace HTTP requests and log key audit events.
  • Environment info: Displays environment variables, configuration properties, active profiles.
  • Custom endpoints: Allows creation of custom endpoints to expose application-specific insights.

Usage: You enable it by adding the spring-boot-starter-actuator dependency, and configure it via application.properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the differences between embedded and traditional web servers in the context of Spring Boot?

A

Embedded Web Servers (used by Spring Boot):
* Server is part of the application: The web server (e.g., Tomcat, Jetty, Undertow) is bundled inside the JAR/WAR and launched by the Spring Boot app itself.
* No need for external server installation: You don’t deploy the app to an existing server — you just run it like any other Java application.
* Simplified deployment: Easier to deploy, especially in containerized environments like Docker.
* Example: Run the app with java -jar myapp.jar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of @ConfigurationProperties, and how does it differ from @Value when injecting configuration?

A

@ConfigurationProperties:
* Purpose: Binds a group of related properties from external sources (like .properties or .yml files) into a structured POJO.
* Usage: Annotate a class with @ConfigurationProperties(prefix = “some.prefix”) and define fields that map to the property keys.
* Supports nested properties and type conversion.
* Ideal for bulk and complex configuration.
* Requires the class to be a Spring bean (@Component or registered via @EnableConfigurationProperties).

@Value:
* Purpose: Injects a single property value into a field, constructor, or method.
* Usage: @Value(“${some.property}”)
* Better for quick or simple value injection.
* Does not support grouping or nesting.
* Less flexible for large config structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly