Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


1. Overview

A common practice in Spring Boot is using an external configuration to define our properties. This allows us to use the same application code in different environments.

We can use properties files, YAML files, environment variables and command-line arguments.

In this short tutorial, we'll explore the main differences between properties and YAML files.

2. Properties Configuration

By default, Spring Boot can access configurations set in an application.properties file, which uses a key-value format:


Here each line is a single configuration, so we need to express hierarchical data by using the same prefixes for our keys. And in this example, every key belongs to spring.datasource.

2.1. Placeholders in Properties

Within our values, we can use placeholders with the ${} syntax to refer to the contents of other keys, system properties, or environment variables:

app.description=${app.name} is a Spring Boot application

2.2. List Structure

If we have the same kind of properties with different values, we can represent the list structure with array indices:


2.3. Multiple Profiles

Since version 2.4.0, Spring Boot supports creating multi-document properties files. Simply put, we can split a single physical file into multiple logical documents.

This allows us to define a document for each profile we need to declare, all in the same file:


Note we use the ‘#---' notation to indicate where we want to split the document.

In this example, we have two spring sections with different profiles tagged. Also, we can have a common set of properties at the root level — in this case, the logging.file.name property will be the same in all profiles.

2.4. Profiles Across Multiple Files

As an alternative to having different profiles in the same file, we can store multiple profiles across different files. Prior to version 2.4.0, this was the only method available for properties files.

We achieve this by putting the name of the profile in the file name — for example, application-dev.yml or application-dev.properties.

3. YAML Configuration

3.1. YAML Format

As well as Java properties files, we can also use YAML-based configuration files in our Spring Boot application. YAML is a convenient format for specifying hierarchical configuration data.

Now let's take the same example from our properties file and convert it to YAML:

        password: password
        url: jdbc:h2:dev
        username: SA

This can be more readable than its property file alternative since it does not contain repeated prefixes.

3.2. List Structure

YAML has a more concise format for expressing lists:

    -   ip: ''
        path: '/path1'
    -   ip: ''
        path: '/path2'
    -   ip: ''
        path: '/path3'

3.3. Multiple Profiles

Unlike properties files, YAML supports multi-document files by design, and this way, we can store multiple profiles in the same file no matter which version of Spring Boot we use.

In this case, however, the spec indicates we have to use three dashes to indicate the start of a new document:

    name: myapplication.log
      on-profile: staging
    password: 'password'
    url: jdbc:h2:staging
    username: SA
  property: stagingValue

Note: We usually don't want to include both the standard application.properties and the application.yml files in our project at the same time, as it could lead to unexpected results.

For instance, if we combine the properties shown above (in an application.yml file) with the properties described in Section 2.3., then bael.property would be assigned with defaultValue instead of with the profile-specific value. This is simply because the application.properties are loaded later on, overriding the values assigned up to that point.

4. Spring Boot Usage

Now that we've defined our configurations, let's see how to access them.

4.1. Value Annotation

We can inject the values of our properties using the @Value annotation:

private String injectedProperty;

Here the property key.something is injected via field injection into one of our objects.

4.2. Environment Abstraction

We can also obtain the value of a property using the Environment API:

private Environment env;

public String getSomeKey(){
    return env.getProperty("key.something");

4.3. ConfigurationProperties Annotation

Finally, we can also use the @ConfigurationProperties annotation to bind our properties to type-safe structured objects:

@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {
    String name;
    String description;

5. Conclusion

In this article, we've seen some differences between properties and yml Spring Boot configuration files. We also saw how their values could refer to other properties. Finally, we looked at how to inject the values into our runtime.

As always, all the code examples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until tomorrow:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!