Let's dive into configurations. Every Spring Boot project starts with the question: "Where should I keep the settings?". Spring Boot answers simply: use application.properties or application.yml. These two files are basically the cockpit for your app, where every line can decide its fate: from a triumphant takeoff to an epic crash.
Spring Boot looks for these files in src/main/resources — the standard place for a project's resources. Makes sense: configuration is a resource too, just a special one — it defines how your app will behave in different situations. And trust me, the right configuration can turn even the simplest app into a reliable enterprise service.
What do we configure?
- Server port: which port the app should run on.
- Database: connection URL, username, password, and so on.
- Application profiles: different settings for dev, test, and prod environments.
- Logging parameters: log levels, output format, etc.
Difference between application.properties and application.yml
You've probably run into the "what's better — JSON or XML?" question, now you're in for similar battles between application.properties and application.yml.
application.properties
This is the "key=value" format, familiar and clear to most developers. Example:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
Advantages:
- Simple and concise.
- Easy to read and edit.
Disadvantages:
- Becomes clunky for complex configurations (for example, arrays).
- Lacks visual structure, which can make big files harder to parse mentally.
application.yml
If properties feel too flat, meet YAML (Yes, that's a recursive acronym: "YAML Ain't Markup Language"). YAML makes configuration more visual thanks to indentation and tree-like structure. Check it out:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
Advantages:
- Supports nested structures, making it more readable.
- Ideal for complex configurations.
Disadvantages:
- Requires careful handling of indentation (wrong indent level — hello, errors).
Actually, the choice between them comes down to your taste and project complexity. But for large applications YAML is often preferred.
Managing profiles in Spring Boot
Imagine your app needs to behave differently in different environments: development (dev), testing (test), production (prod). Spring Boot provides a profiles mechanism for that.
How to set up profiles?
It's simple: add the profile name to your config file. For example:
application-dev.propertiesorapplication-dev.ymlapplication-prod.propertiesorapplication-prod.yml
And in the main application.properties or application.yml file specify the active profile:
spring.profiles.active=dev
Or, if you're a YAML fan:
spring:
profiles:
active: dev
How to change the active profile?
You can also specify the profile via:
- Command-line arguments:
java -jar myapplication.jar --spring.profiles.active=prod - Environment variables:
export SPRING_PROFILES_ACTIVE=prod
This lets you switch between profiles as easily as switching browser tabs.
Types of configuration parameters
Server configuration
Super simple. Want to change the port your embedded server runs on? Easy:
server.port=8081
Database configuration
Connecting to the database won't take long either:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
Logging
To set log levels:
logging.level.org.springframework=INFO
logging.level.com.myapp=DEBUG
logging:
level:
org.springframework: INFO
com.myapp: DEBUG
Remember, good logging is like a good diagram: it explains a lot without overwhelming with details.
Benefits of using YAML
Easier handling of complex structures
YAML lets you define arrays and maps right in the structure:
myapp:
features:
- feature1
- feature2
- feature3
With properties you'd have to write something like this:
myapp.features[0]=feature1
myapp.features[1]=feature2
myapp.features[2]=feature3
Improved readability
YAML with its nested structures (and using spaces instead of . characters) reads easier:
server:
port: 8080
spring:
datasource:
username: root
password: secret
Practical application
Let's do a small example to cement the material. We'll create a simple app that reads configuration parameters from an application.yml file.
Step 1: Create application.yml
server:
port: 8080
app:
title: "Spring Boot Config Demo"
description: "This is our sample app using YAML."
author:
name: "Ivan Ivanov"
email: "ivan@example.com"
Step 2: Reading parameters in code
Spring provides the @Value annotation to inject values from the config file. Example:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
@Value("${app.title}")
private String appTitle;
@Value("${app.description}")
private String appDescription;
@Value("${author.name}")
private String authorName;
@GetMapping("/config")
public String getAppConfig() {
return String.format("%s - %s (author: %s)", appTitle, appDescription, authorName);
}
}
Now, if you run the app you can open http://localhost:8080/config in your browser and see the result.
Common mistakes when working with configurations
- YAML indentation errors. If the file won't parse, make sure you're using spaces, not tabs.
- Extra spaces in
application.properties. Trailing or leading spaces in keys or values can cause unexpected issues. - Incorrect profile specification. If a profile isn't active, its configurations won't be applied.
At this point you should already have solid control over the world of Spring Boot configurations. It's all in your hands! For example, you can configure your dev, test, and production environments so they run like clockwork.
GO TO FULL VERSION