SpringBot application profiles and configuration

SpringBot writes different profiles that can be used to run the application with different sets of configuration options.

SpringBot makes use of the Profiles feature of Spring Boot to allow for the application to be started in different modes with different sets of configuration.

There are four profiles available out of the box:

Profile Comments
dev Most commonly referenced, this profile is used for development, it disables some security to allow for the client-side and server-side to be run seperately.
test Same features as dev however, this profile is configured to use an embedded database and removes all security restrictions on the spring-actuator .
production The production profile, this is the default and is set set if no other profile is defined. It has the strictest security and is recommended for all live environments.

Using a profile

There are a two primary ways to start a SpringBot application with a given profile.

  1. Set the property as a Gradle argument ./gradlew bootRun -P profile={profile}. For example, to use the dev profile, we would run ./gradlew bootRun -P profile=dev More details on this can be see in Running SpringBot.
  2. Setting the SPRING_PROFILES_ACTIVE environment variable value to the profile we wish to run. i.e export SPRING_PROFILES_ACTIVE=dev. This second option is particularly useful when using docker.

Viewing the active profile with the startup console

When starting the application with a given profile set, you will see the applied profiles in the console shown as org.springframework.boot.SpringApplication: The following profiles are active: {profile} .

For example, you can see in the snippet below that we have the test profile.

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::        (v2.3.4.RELEASE)

2020-10-20 07:49:48,569 INFO  [ main ] org.springframework.boot.StartupInfoLogger: Starting MainApplication on 62131feee224 with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace)
2020-10-20 07:49:48,572 INFO  [ main ] org.springframework.boot.SpringApplication: The following profiles are active: test

Viewing the active profile with Spring actuator

Running with the test profile will allow access to the Spring actuator due to it being configured to allow unrestricted access. Using this access, or as an authenticated user we can see which profiles our application is running in by you can also see which profiles are currently active by querying /actuator/env.

Insomnia Spring Boot actuator info/env

Property files

These profiles are defined in property files and can be found in serverside/src/main/resources . Under this directory you can see that there are three files:

├── application-test.properties
├── application-dev.properties
├── application.properties

Protected Regions can be found in all of these files to allow for customisation.

application-test.properties - test profile

SpringBot sets this profile to enable an embedded database in place of the usual remote database. Persistence is set to create-drop , which means the database is cleared after each application restart. Additionally, the test profile sets much more lax security than some of the other profiles.

application-dev.properties - dev profile

This is very similar to the test profile but with some differences; including a different persistence strategy ( update ) which attempts to automatically migrate your database upon each application restart.

application.properties - production

This is the default profile. It is applied in all circumstances with each additional profile being layered on top allowing them to override any of the properties set.

In absence of all other profiles, this one will be run applying the production configuration. This is where security is considered most seriously.

This profile doesn’t need to be set, it will be applied automatically.

Environment variables

Many configuration options can be defined using environment variables which allow for per environment application configurations.

Currently available environment variables are:

Variable Example Description Supported profiles
DATA_SOURCE_URL jdbc:postgresql://localhost:5432/postgres The connection string for your database connection production, dev
DATA_SOURCE_USERNAME codebots The username for your database connection production, dev
DATA_SOURCE_PASSWORD bots The password for your database connection production, dev
HIBERNATE_DDL update The hibernate datasource intialisation mode. See Initialise a database using Spring JDBC for details. production, dev

Adding a new profile

Follow the below steps to create a new profile.

  1. Create a new file in serverside/src/main/resources using the naming convention of application-{profileName}.properties. For example, to create a new profile called beta, this file would be called application-beta.properties.
  2. Add spring.profiles.include=default to the top of your new profiles.
  3. Populate your new profile with your desired configuration options, the recommend process here is to copy an existing one i.e dev and customise as needed.

Ready to start building?

Sign up to Codebots today to see how much faster you can build apps with us.