The application.properties file helps you set up your Spring Boot app's settings in one place. It makes changing app behavior easy without touching code.
Application.properties basics in Spring Boot
Introduction
Syntax
Spring Boot
property.name=value
Each line sets one property with a name and a value separated by =.
Use dot notation to organize properties, like server.port=8080.
Examples
Spring Boot
server.port=8080Spring Boot
spring.datasource.url=jdbc:mysql://localhost:3306/mydbSpring Boot
logging.level.org.springframework=DEBUG
Spring Boot
app.custom.message=Hello from properties!Sample Program
This Spring Boot app reads a custom message from application.properties and shows it at the home page.
Spring Boot
package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class HelloController { @Value("${app.custom.message}") private String message; @GetMapping("/") public String hello() { return message; } }
Important Notes
Remember to place application.properties in src/main/resources folder.
Changes in application.properties require app restart to take effect.
You can override properties with environment variables or command line args.
Summary
application.properties stores app settings in key=value lines.
It helps configure ports, databases, logging, and custom values.
Spring Boot reads these properties automatically when the app starts.
Practice
1. What is the primary purpose of the
application.properties file in a Spring Boot project?easy
Solution
Step 1: Understand the role of
This file is used to store configuration settings in a simple key=value format.application.propertiesStep 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 AQuick 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
Solution
Step 1: Recall Spring Boot property syntax
Spring Boot uses dot notation with key=value pairs inapplication.properties.Step 2: Identify correct key for server port
The correct key isserver.port, and the value is set with an equals sign.Final Answer:
server.port=8081 -> Option AQuick Check:
Dot notation + equals sign = correct syntax [OK]
Hint: Use dot notation and equals sign for properties [OK]
Common Mistakes:
- Using underscores instead of dots
- Using colon instead of equals sign
- Swapping key parts order
3. Given this
What will Spring Boot do with these properties when the app starts?
application.properties snippet:spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=pass123
What will Spring Boot do with these properties when the app starts?
medium
Solution
Step 1: Identify datasource properties
The keys starting withspring.datasourceconfigure database connection details.Step 2: Understand Spring Boot behavior
Spring Boot uses these to connect to the specified MySQL database with username and password.Final Answer:
Connect to MySQL database at localhost with given credentials -> Option DQuick Check:
Datasource properties = DB connection [OK]
Hint: Datasource keys configure DB connection automatically [OK]
Common Mistakes:
- Thinking it starts a web server on DB port
- Assuming properties are ignored
- Believing it creates the database automatically
4. What is wrong with this
application.properties line?server.port-8080
medium
Solution
Step 1: Check property syntax
Properties require key and value separated by an equals sign (=), not a dash (-).Step 2: Validate the key and value
The keyserver.portis correct, and 8080 is a valid port number.Final Answer:
The key and value are separated by a dash instead of equals sign -> Option BQuick Check:
Use '=' to separate key and value [OK]
Hint: Always use '=' between key and value in properties [OK]
Common Mistakes:
- Using dash instead of equals sign
- Swapping key parts order
- Thinking spaces fix the syntax
5. You want to add a custom property
app.title in application.properties and access it in your Spring Boot code. Which of these steps is correct?hard
Solution
Step 1: Add custom property correctly
Use key=value syntax:app.title=MyAppinapplication.properties.Step 2: Access property in Spring Boot code
Use@Value("${app.title}")annotation to inject the property value.Final Answer:
Addapp.title=MyAppin properties and use@Value("${app.title}")in code -> Option CQuick Check:
Custom property + @Value injection = correct [OK]
Hint: Use @Value("${key}") to inject properties in code [OK]
Common Mistakes:
- Using colon instead of equals in properties
- Trying to get property from environment variables
- Incorrect use of @Autowired for simple values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
