Understanding application.properties Structure in Spring Boot
📖 Scenario: You are setting up a simple Spring Boot application. You need to configure basic settings like server port and application name using the application.properties file.
🎯 Goal: Learn how to create and structure an application.properties file with key-value pairs to configure a Spring Boot application.
📋 What You'll Learn
Create an application.properties file with specific properties
Add a property for server.port with value 8081
Add a property for spring.application.name with value MyApp
Add a property for logging.level.root with value INFO
💡 Why This Matters
🌍 Real World
Spring Boot applications use application.properties to configure settings without changing code. This helps manage environments like development and production easily.
💼 Career
Knowing how to configure application.properties is essential for backend developers working with Spring Boot to customize app behavior and troubleshoot issues.
Progress0 / 4 steps
1
Create the application.properties file
Create a file named application.properties and add the property server.port=8081 to set the server port.
Spring Boot
Hint
The application.properties file uses key=value pairs. Start by setting server.port=8081.
2
Add application name property
Add the property spring.application.name=MyApp below the existing server.port property in application.properties.
Spring Boot
Hint
Add a new line with spring.application.name=MyApp to name your application.
3
Add logging level property
Add the property logging.level.root=INFO below the existing properties in application.properties to set the default logging level.
Spring Boot
Hint
Set the logging level by adding logging.level.root=INFO on a new line.
4
Complete the application.properties structure
Ensure the application.properties file contains exactly these three properties in order: server.port=8081, spring.application.name=MyApp, and logging.level.root=INFO.
Spring Boot
Hint
Check that all three properties are present and correctly written in the file.
Practice
(1/5)
1. What is the primary purpose of the application.properties file in a Spring Boot project?
easy
A. To store configuration settings as key=value pairs
B. To write Java code for the application
C. To define database tables
D. To create HTML templates
Solution
Step 1: Understand the role of application.properties
This file is used to hold configuration settings for the Spring Boot app.
Step 2: Identify the format and purpose
It stores settings as simple key=value pairs to configure app behavior without code changes.
Final Answer:
To store configuration settings as key=value pairs -> Option A
Quick Check:
Config file = key=value pairs [OK]
Hint: Remember: properties file = settings in key=value form [OK]
Common Mistakes:
Confusing properties file with source code files
Thinking it stores HTML or database schema
Assuming it runs application logic
2. Which of the following is the canonical syntax to set the server port to 8081 in application.properties?
easy
A. server-port=8081
B. server_port=8081
C. server.port=8081
D. serverPort=8081
Solution
Step 1: Recall the key naming convention
Spring Boot uses dot notation to group related settings, so keys use dots, not dashes or underscores.
Step 2: Match the correct syntax
The correct key for server port is server.port, followed by = and the port number.
Final Answer:
server.port=8081 -> Option C
Quick Check:
Dots separate groups in keys = correct syntax [OK]
Hint: Use dots, not dashes or underscores in keys [OK]