CodeGym /Courses /Module 5. Spring /Lecture 174: Setting up Spring Boot microservices with co...

Lecture 174: Setting up Spring Boot microservices with configs in application.yml

Module 5. Spring
Level 12 , Lesson 3
Available

It's time to dive deeper into configuring Spring Boot apps. Today we'll learn how to organize configs using application.yml, go over the advantages of YAML over .properties, master multi-profile configs, and understand how to apply different settings for different environments (like dev, test, prod).


What is application.yml?

When we studied Docker Compose, we already encountered YAML (YAML Ain't Markup Language). Quick reminder: it's a convenient format for writing configuration files. Its main advantages:

  • Readability: less "noise" than .properties (no repeated keys for grouped settings).
  • Hierarchical structure: configuration is easy to group as a tree.
  • Support for complex objects: lists and nested data structures.

For comparison, configuration in .properties:


server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret

And the same configuration in application.yml:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret

Admit it, application.yml looks less cluttered. Spring Boot supports both formats, but YAML is used more often in modern projects.


How Spring Boot works with configurations

Spring Boot automatically loads configuration files from src/main/resources. The most common configuration file is application.yml. It lets you set app settings like:

  • Server port
  • Database connection settings
  • Caching and security settings

Spring Boot resolves properties from the file and applies them automatically to the corresponding components.


Benefits of using application.yml

Using application.yml gives a few key benefits:

  1. Hierarchical structure
    • Related parameters are easy to group into a single section (e.g., database settings).
    • Readable format for complex configurations.
  2. Profile support
    • Convenient management of settings for different environments — dev, test, prod.
  3. List support
    • For example, you can specify a list of URLs in the configuration.

Example of a list:

my-service:
  servers:
    - http://localhost:8081
    - http://localhost:8082
    - http://localhost:8083

Structuring and grouping settings

The YAML hierarchy lets you logically organize configuration parameters.

Server settings

Let's set the port for our microservice:


server:
port: 8080

You can also set properties related to encoding:

server:
  port: 8080
  spring:
    messages:
      encoding: UTF-8

Database settings

Connecting to a database is one of the most common tasks in Spring Boot. Let's look at a configuration:


  spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  • url — the database address.
  • ddl-auto — schema management strategy (e.g., update, create-drop).
  • show-sql — enables printing SQL queries to logs.

Configuration profiles

In real projects we usually have different configs for development (dev), testing (test) and production (prod). For example:

  • In dev we use the H2 in-memory database.
  • In prod we use MySQL.

Spring Boot lets you specify such settings via profiles.

The active profile is specified in application.yml:

spring:
  profiles:
    active: dev

Or you can pass the profile as a command-line argument:


java -Dspring.profiles.active=prod -jar myapp.jar

Splitting configuration by profile

Each profile can have its own config file, for example:

  • application-dev.yml
  • application-prod.yml

Example application-dev.yml:


  spring:
  datasource:
    url: jdbc:h2:mem:devdb
    username: sa
    password:
  jpa:
    hibernate:
      ddl-auto: create

Example application-prod.yml:

spring:
  datasource:
    url: jdbc:mysql://prod-db:3306/mydb
    username: admin
    password: securepassword
  jpa:
    hibernate:
      ddl-auto: validate

Default configuration

The main application.yml can contain common settings:


  server:
  port: 8080
spring:
  jpa:
    show-sql: true

Spring Boot loads application.yml first, then the profile-specific settings (application-dev.yml or application-prod.yml), overriding properties as needed.


Hands-on: configuring a microservice with application.yml

Step 1: Create a new project

Use Spring Initializr to create the project:
Dependencies: Spring Web, Spring Data JPA, MySQL Driver.

Remember the joke: "If you don't use Spring Initializr, you must really love pain. It's there so you can hit the 'Generate' button".

Step 2: Configure application.yml

Add application.yml to src/main/resources:


server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Step 3: Add profiles

Create application-dev.yml and application-prod.yml.

Example: application-dev.yml:

spring:
  datasource:
    url: jdbc:h2:mem:devdb
    username: sa
    password:
  jpa:
    hibernate:
      ddl-auto: create

Example: application-prod.yml:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://prod-db:3306/mydb
    username: admin
    password: securepassword

Set the active profile:


  spring:
  profiles:
    active: dev

Step 4: Implement a simple REST API

Create a controller:


@RestController
@RequestMapping("/api/customers")
public class CustomerController {

    @GetMapping
    public String getAllCustomers() {
        return "List of all customers!";
    }
}

Step 5: Run the application

Start the app and open:
http://localhost:8081/api/customers.


Common pitfalls

Some things that often cause trouble:

  1. Conflicting configurations: if application.yml and a profile-specific file contain conflicting settings, the active profile will override the main configuration.
  2. Missing active profile: don't forget to set the active profile via spring.profiles.active.
  3. YAML formatting errors: the slightest indentation mistake can break the app.

For deploying to real servers you usually pass the profile via JVM args (-Dspring.profiles.active=prod).


Now you know how to configure application.yml, effectively split configurations into profiles, and apply them in microservices. YAML is now your buddy on this journey!

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION