Sometimes you want to configure your Spring Boot app using a different file format than application.yml. This helps when you prefer simpler or more familiar formats.
0
0
application.yml alternative in Spring Boot
Introduction
You want to use <code>application.properties</code> instead of YAML for easier editing.
You need to override settings in environment variables for cloud deployment.
You want to use command line arguments to quickly change config without files.
You prefer JSON format for configuration in some cases.
You want to externalize config in a custom file for different environments.
Syntax
Spring Boot
spring.application.name=MyApp
server.port=8080
logging.level.root=INFOapplication.properties uses key=value pairs, one per line.
Keys use dot notation to represent nested properties.
Examples
Basic properties file with app name and port.
Spring Boot
spring.application.name=MyApp
server.port=8080Set logging level for Spring framework to DEBUG.
Spring Boot
logging.level.org.springframework=DEBUG
Database connection settings in properties format.
Spring Boot
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secretSample Program
This Spring Boot app reads the app name from application.properties and shows it on the home page.
Spring Boot
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; 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("${spring.application.name}") private String appName; @GetMapping("/") public String home() { return "Welcome to " + appName + "!"; } }
OutputSuccess
Important Notes
You can use application.properties as a simple alternative to application.yml.
Environment variables and command line args can also override these settings.
YAML supports complex structures better, but properties files are easier for simple configs.
Summary
application.properties is a common alternative to application.yml.
It uses simple key=value lines for configuration.
You can choose the format that fits your project and team best.