Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Settings: properties and YAML

The server port, the database address, the site name — none of these belong hard-coded. Such values live in a settings file, and Spring Boot reads it at startup. Then the same program can run with different settings on your computer and on the server.

Two formats

The generator created application.properties. The same settings can be written in YAML:

# src/main/resources/application.properties
spring.application.name=blog
server.port=8080
# src/main/resources/application.yaml
spring:
  application:
    name: blog
server:
  port: 8080

In YAML the dot-separated parts of a name become nested blocks indented by two spaces. Spring reads both files the same way. From here on the course uses YAML: it is easier to read with many settings and, unlike .properties, it is read as UTF-8, so non-English text does not get garbled. Rename the file to application.yaml and rewrite its content as above.

Your own settings

Besides Spring's settings you can add your own. Let's agree that blog settings start with blog:

# src/main/resources/application.yaml
spring:
  application:
    name: blog
 
blog:
  title: Anna and Oleg blog
  contact-email: anna@example.com

Reading with @Value

// src/main/java/com/example/blog/AboutController.java
package com.example.blog;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class AboutController {
 
    private final String title;
    private final String contactEmail;
 
    public AboutController(@Value("${blog.title}") String title,
                           @Value("${blog.contact-email:info@example.com}") String contactEmail) {
        this.title = title;
        this.contactEmail = contactEmail;
    }
 
    @GetMapping("/api/about")
    public String about() {
        return title + " (" + contactEmail + ")";
    }
}

Spring replaces the ${blog.title} expression with the value from the settings. After a colon you can give a default value — it is used when the setting is missing. The request curl http://localhost:8080/api/about returns Anna and Oleg blog (anna@example.com).

Overriding at startup

Any setting can be changed without touching the file:

./mvnw spring-boot:run -Dspring-boot.run.arguments=--server.port=9090
BLOG_TITLE="Test blog" ./mvnw spring-boot:run

The first command passes a command-line argument, the second an environment variable. The variable name comes from the setting name: dots and dashes become _, letters become upper case (blog.title → BLOG_TITLE).

SourcePriority
Command-line arguments --key=valueHighest
Environment variablesAbove the file
application.yaml and application.propertiesBase values

Common mistake

If you put a non-ASCII character into application.properties, it reaches the program garbled. The value Anna's Café comes out as:

Anna's Café (info@example.com)

Spring Boot reads .properties files in the old ISO-8859-1 encoding, and YAML in UTF-8. Keep such text in application.yaml. And do not keep both files at once: for the same key the .properties value wins, and after renaming run ./mvnw clean so the old file does not linger in the target folder.

Thanks for reading!

Donate to the site — we work hard for you.