Master Spring Boot Configuration: From Properties to Multi‑Environment Setup

This tutorial explains how Spring Boot loads configuration files, compares properties and YAML formats, shows how to define custom parameters, use random values, switch environments via profiles, and leverage the new Binder API with practical code examples for Java developers.

Programmer DD
Programmer DD
Programmer DD
Master Spring Boot Configuration: From Properties to Multi‑Environment Setup

Introduction

Spring Boot simplifies the creation of web applications by providing automatic configuration. Most settings are placed in src/main/resources and can be defined in application.properties or application.yml.

Configuration Basics

The default configuration file is src/main/resources/application.properties. You can set properties such as the server port with: server.port=8888 YAML is also supported and often more readable:

server:
  port: 8888

Random Values

Spring Boot can generate random values using the ${random.*} placeholder. Examples: ${random.int} – a random integer ${random.int[10,20]} – an integer between 10 and 20 ${random.uuid} – a random UUID

Custom Parameters

You can add your own properties, for example:

book.name=SpringCloudInAction
book.author=ZhaiYongchao

These values can be injected with @Value:

@Component
public class Book {
    @Value("${book.name}")
    private String name;
    @Value("${book.author}")
    private String author;
    // getters and setters omitted
}

Parameter References

Properties can reference each other using the ${...} placeholder:

book.desc=${book.author} is writing "${book.name}"

Command‑Line Arguments

Command‑line arguments can override configuration, e.g.: java -jar app.jar --server.port=8888 This is equivalent to adding server.port=8888 in application.properties.

Multi‑Environment Configuration

Spring Boot supports profile‑specific files named application-{profile}.properties or application-{profile}.yml. Activate a profile with: spring.profiles.active=dev Typical setup: application-dev.properties – port 1111 application-test.properties – port 2222 application-prod.properties – port 3333

The default profile can be set in application.properties:

spring.profiles.active=dev

Loading Order

Spring Boot resolves properties in a defined order (high to low):

Command‑line arguments SPRING_APPLICATION_JSON environment variable

JNDI attributes

Java system properties

OS environment variables

Random‑generated values

External application-{profile}.properties / .yml Internal application-{profile}.properties /

.yml
@ConfigurationProperties

with @PropertySource Default properties defined via

SpringApplication.setDefaultProperties

New Binding API (Spring Boot 2.x)

Spring Boot 2 introduces the Binder API for programmatic binding.

Simple Type Example

@Data
@ConfigurationProperties(prefix="com.didispace")
public class FooProperties {
    private String foo;
}

Binding with Binder:

ApplicationContext ctx = SpringApplication.run(Application.class, args);
Binder binder = Binder.get(ctx.getEnvironment());
FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
System.out.println(foo.getFoo());

List Type Example

List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
System.out.println(post);

Binding a list of custom objects:

List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
System.out.println(posts);

Conclusion

Spring Boot’s flexible configuration system lets you manage simple properties, complex YAML structures, random values, and profile‑specific settings, while the new Binder API provides a clean way to access configuration programmatically.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

YAMLpropertiesspring-bootProfiles
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.