Bird
Raised Fist0
Spring Bootframework~10 mins

Custom configuration properties in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Custom configuration properties
Define POJO class with fields
↓
Annotate class with @ConfigurationProperties
↓
Enable configuration properties in main app or config
↓
Add properties in application.yml or application.properties
↓
Spring Boot reads properties at startup
↓
Spring injects values into POJO fields
↓
Use POJO bean in your app code
This flow shows how Spring Boot reads custom properties from config files and injects them into a POJO class for easy use.
Execution Sample
Spring Boot
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app.settings")
public class AppSettings {
  private String name;
  private int timeout;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getTimeout() {
    return timeout;
  }

  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
}
Defines a POJO class to hold custom properties with prefix 'app.settings' from config files.
Execution Table
StepActionEvaluationResult
1Spring Boot startsReads application.properties or application.ymlLoads all properties into environment
2Detect @ConfigurationProperties on AppSettingsPrefix = 'app.settings'Prepare to bind matching properties
3Find properties: app.settings.name=MyAppMatches 'name' fieldBind 'MyApp' to name
4Find properties: app.settings.timeout=30Matches 'timeout' fieldBind 30 to timeout
5Create AppSettings bean with bound valuesBean ready for injectionBean with name='MyApp', timeout=30
6Use AppSettings bean in app codeCall getName() and getTimeout()Returns 'MyApp' and 30
7No more properties to bindBinding completeSpring Boot finishes startup
💡 All matching properties bound to POJO fields, no more properties left
Variable Tracker
VariableStartAfter Step 3After Step 4Final
namenull"MyApp""MyApp""MyApp"
timeout003030
Key Moments - 3 Insights
Why do we need the @ConfigurationProperties annotation?
It tells Spring Boot which class to bind properties to, as shown in execution_table step 2 where Spring detects the annotation and prepares binding.
What happens if a property in the config file does not match any field in the POJO?
Spring Boot ignores unmatched properties during binding, so only matching fields get values, as seen in execution_table steps 3 and 4.
How does Spring Boot know which properties to bind to which fields?
It uses the prefix from @ConfigurationProperties and matches property names to field names, demonstrated in execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does the 'name' field have after step 3?
A"MyApp"
Bnull
C"app.settings.name"
D30
💡 Hint
Check the 'name' variable value in variable_tracker after step 3
At which step does Spring Boot create the AppSettings bean with bound values?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look for the step mentioning bean creation in execution_table
If the property 'app.settings.timeout' was missing, what would be the final value of 'timeout'?
Anull
B0
C30
DThrows error
💡 Hint
See variable_tracker start value and binding steps for 'timeout'
Concept Snapshot
Custom configuration properties in Spring Boot:
- Create a POJO class with fields
- Annotate with @ConfigurationProperties(prefix = "your.prefix")
- Register as @Component or enable via @EnableConfigurationProperties
- Define matching properties in application.yml or .properties
- Spring Boot binds values at startup
- Use the POJO bean anywhere in your app
Full Transcript
This visual execution trace shows how Spring Boot handles custom configuration properties. First, you create a POJO class with fields for your settings. You annotate it with @ConfigurationProperties and specify a prefix. Spring Boot scans your config files like application.properties or application.yml for properties matching that prefix. It then binds those property values to the fields in your POJO. The POJO is registered as a Spring bean and can be injected anywhere in your app. The execution table walks through each step: starting Spring Boot, detecting the annotation, binding each property, creating the bean, and using it. The variable tracker shows how each field changes from default to bound values. Key moments clarify why the annotation is needed, how unmatched properties are ignored, and how matching works. The quiz tests your understanding of these steps. This method helps keep configuration clean, typed, and easy to use in Spring Boot applications.

Practice

(1/5)
1. What is the main purpose of using @ConfigurationProperties in Spring Boot?
easy
A. To create REST endpoints automatically
B. To enable security features in the application
C. To define database connection pools
D. To bind external configuration properties to a Java class

Solution

  1. Step 1: Understand the role of @ConfigurationProperties

    This annotation is used to map external configuration values (from files like application.properties or application.yml) to fields in a Java class.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other Spring Boot features but not to property binding.
  3. Final Answer:

    To bind external configuration properties to a Java class -> Option D
  4. Quick Check:

    Property binding = B [OK]
Hint: Remember: @ConfigurationProperties binds config files to classes [OK]
Common Mistakes:
  • Confusing @ConfigurationProperties with @RestController
  • Thinking it manages database connections
  • Assuming it enables security automatically
2. Which of the following is the correct way to enable @ConfigurationProperties support in a Spring Boot application?
easy
A. Add @SpringBootApplication on the properties class
B. Add @EnableConfigurationProperties on a configuration class or main class
C. Add @RestController on the properties class
D. Add @ComponentScan on the properties class

Solution

  1. Step 1: Identify how to activate configuration properties

    Spring Boot requires @EnableConfigurationProperties on a configuration or main class to enable binding of @ConfigurationProperties classes.
  2. Step 2: Review other annotations

    @ComponentScan scans components but does not enable properties binding. @RestController is for web controllers. @SpringBootApplication is for main app class but does not alone enable properties binding.
  3. Final Answer:

    Add @EnableConfigurationProperties on a configuration class or main class -> Option B
  4. Quick Check:

    Enable config properties = D [OK]
Hint: Use @EnableConfigurationProperties to activate property binding [OK]
Common Mistakes:
  • Confusing @ComponentScan with enabling properties
  • Adding @RestController on properties class
  • Assuming @SpringBootApplication alone enables properties
3. Given the following properties in application.yml and class, what will be the output of System.out.println(appProperties.getTimeout())?
application.yml:
app:
  timeout: 5000

Java class:
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private int timeout;
    public int getTimeout() { return timeout; }
    public void setTimeout(int timeout) { this.timeout = timeout; }
}
medium
A. null
B. 0
C. 5000
D. Compilation error

Solution

  1. Step 1: Understand property binding from YAML to class

    The app.timeout property in YAML is set to 5000. The class with @ConfigurationProperties(prefix = "app") binds this value to the timeout field.
  2. Step 2: Check getter method output

    Calling getTimeout() returns the bound value 5000, so the printed output is 5000.
  3. Final Answer:

    5000 -> Option C
  4. Quick Check:

    Bound value printed = 5000 [OK]
Hint: Property prefix matches class prefix to bind values correctly [OK]
Common Mistakes:
  • Forgetting to add setter method causes binding failure
  • Using wrong prefix in annotation
  • Expecting null or default instead of bound value
4. Identify the error in this custom properties class that prevents property binding:
@ConfigurationProperties(prefix = "server")
public class ServerProperties {
    private String port;
    public String getPort() { return port; }
    // Missing setter method
}
medium
A. Missing setter method for 'port' prevents binding
B. Prefix 'server' is invalid and causes error
C. Class must be annotated with @Component
D. Field 'port' should be public for binding

Solution

  1. Step 1: Check property binding requirements

    Spring Boot binds properties using setters. Without a setter for 'port', the value cannot be set.
  2. Step 2: Validate other options

    The prefix 'server' is valid. The class does not require @Component if registered via @EnableConfigurationProperties. Fields should be private with getters/setters, not public.
  3. Final Answer:

    Missing setter method for 'port' prevents binding -> Option A
  4. Quick Check:

    Setter missing = binding fails [OK]
Hint: Always add setters for fields to enable binding [OK]
Common Mistakes:
  • Assuming prefix must be custom or different
  • Thinking @Component is always required
  • Making fields public instead of using setters
5. You want to create a nested configuration property for a mail server with host and port inside application.properties like this:
mail.server.host=smtp.example.com
mail.server.port=587
Which of the following Java classes correctly models this nested property using @ConfigurationProperties?
hard
A. @ConfigurationProperties(prefix = "mail") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server }
B. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private String host; private int port; // getters and setters }
C. @ConfigurationProperties(prefix = "mail") public class MailProperties { private String host; private int port; // getters and setters }
D. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server }

Solution

  1. Step 1: Understand nested property binding

    Properties like mail.server.host require a class with prefix mail and a nested class Server to hold host and port.
  2. Step 2: Analyze options

    @ConfigurationProperties(prefix = "mail") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server } correctly uses @ConfigurationProperties(prefix = "mail") with a nested Server class. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private String host; private int port; // getters and setters } flattens properties incorrectly. @ConfigurationProperties(prefix = "mail") public class MailProperties { private String host; private int port; // getters and setters } misses nesting. @ConfigurationProperties(prefix = "mail.server") public class MailProperties { private Server server; public static class Server { private String host; private int port; // getters and setters } // getters and setters for server } uses prefix mail.server but still nests, which is inconsistent.
  3. Final Answer:

    @ConfigurationProperties(prefix="mail") with nested Server class -> Option A
  4. Quick Check:

    Nested class with prefix mail = A [OK]
Hint: Use nested classes for nested property groups [OK]
Common Mistakes:
  • Using wrong prefix for nested properties
  • Flattening nested properties into one class
  • Missing getters/setters in nested class
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard