Spring Boot – Profiling Application Properties

In nearly every Software I developed, the development and the production system differ in a few things. Of course every IT Architect explains, that Development and Real Life shouldn´t differ, but they do.
Just as a simple example, think about that you have to use a different port on your local machine, than on the server.
Or maybe you are using a other database (for example in memory database) in your Test-Environment, but need to use a PostgreSQL Database in your production system.
We can have a thousand of different examples.

But for now, lets think of a simple example, with different ports for development and production.

After you have made the basic setup for your Spring Boot Project, we need 3 different application configuration files

Naming Convention

We use the different configuration files to check dynamically which profile we want to use. To accomplish this, please follow the following naming convention: 

application-<yourProfil>.yml (or .properties)

application.yml

spring:
  profiles:
    active: '@spring.profile@'

In here we just define that we want to use a special profile. If we won´t select one the project will be intialized as default. Please don´t forget the ‘….’ 

application-prod.yml

server: 
        port: 8181

application-dev.yml

server: 
        port: 9191

That´s all. Now we can define to which port our application should listen.

Note:

If you define the port in the application.yml, it will always be used this port.
The application.yml overrides all configurations of profiled configurations

Project Structure

After you have added all this configuration files, your structure should look like this

Activate Profile

We´ll start this application with a specific profile by

mvn spring-boot:run -Dspring.profiles.active=prod

 

Thanks for reading and have fun using Spring Boot Application Profiles.

Feel free to share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.