Spring Boot Externalized Configuration and Profiles Questions and Answers — Questions and Answers
Question 1: A Spring Boot application defines the property `server.port=8080` in its `application.properties` file. An OS environment variable is set as `SERVER_PORT=7070`. The application is then launched with the command: `java -jar app.jar --server.port=9090`. On which port will the application start?
- 8080
- 7070
- 9090 (Correct answer)
- The application will fail to start due to a configuration conflict.
Correct answer: 9090
Spring Boot uses a specific order of precedence for externalized configuration. Command-line arguments have a higher priority than both OS environment variables and properties defined in `application.properties`. Therefore, the value `9090` from the command-line argument will override the other values.
Question 2: A developer needs to create a configuration bean that is only active when the `production` profile is NOT active. Which annotation should be used on the bean's definition to achieve this?
- @Profile("not production")
- @Profile("!production") (Correct answer)
- @ConditionalOnMissingProfile("production")
- @Profile("default")
Correct answer: @Profile("!production")
The `@Profile` annotation supports a NOT operator, represented by the exclamation mark `!`. Placing `@Profile("!production")` on a bean ensures it will be registered in the application context only when the 'production' profile is not active.
Question 3: A developer wants to bind a group of related properties under the prefix `app.credentials` to a type-safe POJO class. Which annotation is the most idiomatic and robust way to achieve this in Spring Boot?
- Using individual `@Value` annotations for each field in the POJO.
- Manually binding properties from the `Environment` object.
- Using `@ConfigurationProperties(prefix = "app.credentials")` on the POJO. (Correct answer)
- Using `@PropertySource` to load the properties into the class.
Correct answer: Using `@ConfigurationProperties(prefix = "app.credentials")` on the POJO.
`@ConfigurationProperties` is specifically designed for binding structured, hierarchical properties to a dedicated object in a type-safe manner. It is superior to using multiple `@Value` annotations for related properties as it provides better encapsulation, validation, and is less error-prone.
Question 4: An application has an `application-dev.properties` file with `logging.level.root=DEBUG` and an `application-prod.properties` file with `logging.level.root=WARN`. If the application is launched with `spring.profiles.active=dev,prod`, what will be the effective root logging level?
- DEBUG
- WARN (Correct answer)
- INFO (the default)
- The application will fail due to conflicting properties.
Correct answer: WARN
When multiple profiles are active, properties from profiles declared later in the list take precedence. Since `prod` is the last profile in the `dev,prod` list, its `logging.level.root` value of `WARN` will override the value from the `dev` profile. This is known as the "last-one-wins" strategy.
Question 5: Which of the following `application.yml` snippets correctly configures a list of strings for the property `myapp.api-keys`?
- myapp.api-keys: [key1, key2, key3]
- myapp: api-keys: - key1 - key2 - key3 (Correct answer)
- myapp: api-keys: key1, key2, key3
- myapp.api-keys[0]=key1 myapp.api-keys[1]=key2 myapp.api-keys[2]=key3
Correct answer: myapp: api-keys: - key1 - key2 - key3
YAML uses a hyphen (`-`) followed by a space to denote items in a list. Each item is typically on a new line and indented under the parent key.
Question 6: A developer injects a property into an integer field using `@Value("${app.connection.pool-size:30}")`. If the `app.connection.pool-size` property is not defined in any configuration source, what will be the outcome?
- The application will fail to start because the property cannot be resolved.
- The `pool-size` field will be `null`.
- The `pool-size` field will be initialized to `0`.
- The `pool-size` field will be initialized to `30`. (Correct answer)
Correct answer: The `pool-size` field will be initialized to `30`.
The colon (`:`) syntax within a `@Value` expression provides a default value. If the property `app.connection.pool-size` is not found, the default value `30` will be used. Spring's type conversion service will automatically convert the string "30" to an integer.
A Spring Boot application defines the property `server.port=8080` in its `application.properties` file.
An OS environment variable is set as `SERVER_PORT=7070`.
The application is then launched with the command: `java -jar app.jar --server.port=9090`.
On which port will the application start?