Using application.properties as an application.yml Alternative in Spring Boot
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
Many Spring Boot projects use application.properties files for configuration because they are simple and widely supported. This project shows how to switch from YAML to properties format.
💼 Career
Understanding how to configure Spring Boot applications with properties files is essential for backend developers working with Java and Spring frameworks in real-world projects.
Progress0 / 4 steps
1
Create application.properties file with app.title
Create a file named application.properties and add the property app.title=My Spring App inside it.
Spring Boot
Hint
The application.properties file uses key=value pairs. Write app.title=My Spring App exactly.
2
Create a Spring component to read app.title property
Create a Spring component class named AppConfig with a field title that reads the app.title property using @Value("${app.title}") annotation.
Spring Boot
Hint
Use @Component on the class and @Value("${app.title}") on the title field. Add a getter method.
3
Inject AppConfig into main application and print title
In the main Spring Boot application class named DemoApplication, inject AppConfig using constructor injection. Add a CommandLineRunner bean that prints the title property value from AppConfig.
Spring Boot
Hint
Use a @Bean method returning CommandLineRunner that prints appConfig.getTitle(). Include the main method to run the app.
4
Complete the Spring Boot application setup
Ensure the DemoApplication class has the @SpringBootApplication annotation and the main method to start the application.
Spring Boot
Hint
Make sure the DemoApplication class has @SpringBootApplication and a main method that calls SpringApplication.run.
Practice
(1/5)
1. Which file format is a common alternative to application.yml in Spring Boot for configuration?
easy
A. config.ini
B. config.xml
C. settings.json
D. application.properties
Solution
Step 1: Understand Spring Boot configuration files
Spring Boot supports multiple configuration file formats, but the two most common are application.yml and application.properties.
Step 2: Identify the alternative format
The application.properties file uses simple key=value pairs and is widely used as an alternative to YAML files.
Final Answer:
application.properties -> Option D
Quick Check:
Alternative to application.yml = application.properties [OK]
Hint: Remember: properties files use key=value lines [OK]
Common Mistakes:
Confusing XML or JSON as default Spring Boot config
Thinking config.ini is supported by Spring Boot
Assuming YAML is the only option
2. Which of the following is the correct syntax for setting a server port in application.properties?
easy
A. server.port=8080
B. server.port => 8080
C. server.port : 8080
D. server.port -> 8080
Solution
Step 1: Recall properties file syntax
In application.properties, each configuration is set as a key=value pair without colons or arrows.
Step 2: Identify the correct key-value format
The correct syntax uses an equals sign: server.port=8080.
Final Answer:
server.port=8080 -> Option A
Quick Check:
Properties syntax uses = not : or arrows [OK]
Hint: Properties files always use key=value format [OK]
Common Mistakes:
Using colon instead of equals sign
Using arrow symbols which are invalid
Mixing YAML syntax with properties syntax
3. Given the following application.properties snippet:
B. It configures a MySQL database connection with the specified URL and credentials.
C. It disables the datasource configuration.
D. It causes a syntax error and fails to start.
Solution
Step 1: Analyze the datasource properties
The keys spring.datasource.url, username, and password configure the database connection details.
Step 2: Identify the database type and credentials
The URL contains jdbc:mysql://, indicating a MySQL database with host localhost and database mydb. Username and password are set accordingly.
Final Answer:
It configures a MySQL database connection with the specified URL and credentials. -> Option B
Quick Check:
Datasource URL with jdbc:mysql = MySQL config [OK]
Hint: Look for jdbc:mysql in URL to identify MySQL config [OK]
Common Mistakes:
Assuming PostgreSQL from MySQL URL
Thinking this disables datasource
Confusing syntax as error
4. You wrote this in application.properties:
server.port9090
Why does your Spring Boot app fail to start on port 9090?
medium
A. Spaces are not allowed anywhere in properties files.
B. Port 9090 is reserved and cannot be used.
C. Missing equals sign between key and value causes syntax error.
D. Property name is incorrect; should be server.port.number.
Solution
Step 1: Check properties file syntax
Each line must have a key and value separated by an equals sign (=) or colon (:).
Step 2: Identify the missing equals sign
The line server.port9090 lacks the equals sign, causing it to be parsed as a single key with no value and preventing the port from being set.
Final Answer:
Missing equals sign between key and value causes syntax error. -> Option C
Quick Check:
Properties need key=value format [OK]
Hint: Always use = or : between key and value [OK]
Common Mistakes:
Omitting equals sign
Using wrong property name
Assuming spaces are forbidden everywhere
5. You want to migrate your Spring Boot app configuration from application.yml to application.properties. Which of the following is the correct way to convert this YAML snippet?