📖 Scenario: You are creating a simple Spring Boot application that needs configuration settings for the server port and application name.
🎯 Goal: Learn how to set up and use the application.properties file to configure basic Spring Boot application properties.
📋 What You'll Learn
Create an application.properties file with specific properties
Add a property for server.port with value 8085
Add a property for spring.application.name with value MyApp
Access these properties in a Spring Boot component using @Value annotation
Print the configured values inside a CommandLineRunner bean
💡 Why This Matters
🌍 Real World
Setting application properties is essential for configuring Spring Boot apps without changing code. It helps manage environments and customize behavior.
💼 Career
Understanding application.properties and property injection is a fundamental skill for Spring Boot developers working on real-world backend services.
Progress0 / 4 steps
1
Create application.properties file with server port
Create an application.properties file and add the line server.port=8085 to set the server port.
Spring Boot
Hint
The application.properties file is where Spring Boot reads configuration values. Setting server.port changes the port your app runs on.
2
Add application name property
In the same application.properties file, add the line spring.application.name=MyApp to set the application name.
Spring Boot
Hint
The spring.application.name property helps identify your app in logs and monitoring.
3
Create a Spring Boot component to read properties
Create a Java class named AppConfigReader annotated with @Component. Use @Value to inject server.port into an int serverPort field and spring.application.name into a String appName field.
Spring Boot
Hint
Use @Value("${property.name}") to inject property values into fields.
4
Print properties using CommandLineRunner
In your main Spring Boot application class, create a CommandLineRunner bean that injects AppConfigReader and prints the serverPort and appName values.
Spring Boot
Hint
Use a CommandLineRunner bean to run code after the app starts. Inject your AppConfigReader to access properties.
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 HTML templates for the UI
D. To manage database schema migrations
Solution
Step 1: Understand the role of application.properties
This file is used to store configuration settings in a simple key=value format.
Step 2: Compare with other options
Writing Java code, defining HTML templates, or managing database migrations are done elsewhere, not in this file.
Final Answer:
To store configuration settings as key=value pairs -> Option A
Quick Check:
Config file = key=value pairs [OK]
Hint: Remember: properties file holds settings, not code or templates [OK]
Common Mistakes:
Thinking it contains Java code
Confusing it with HTML or template files
Assuming it manages database migrations
2. Which of the following is the correct 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. port.server=8081
Solution
Step 1: Recall Spring Boot property syntax
Spring Boot uses dot notation with key=value pairs in application.properties.
Step 2: Identify correct key for server port
The correct key is server.port, and the value is set with an equals sign.
Final Answer:
server.port=8081 -> Option A
Quick Check:
Dot notation + equals sign = correct syntax [OK]
Hint: Use dot notation and equals sign for properties [OK]