Start modelling your app today.

Get started for free

What's this?

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: default,{profile}.

For example, you can see in the snippet below that we have the default (always applied) and the test profile.

Setting Active Processor Count to 8
WARNING: Container memory limit unset. Configuring JVM for 1G container.
Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx385788K -XX:MaxMetaspaceSize=150787K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 1G, Thread Count: 250, Loaded Class Count: 24208, Headroom: 0%)
Adding 127 container CA certificates to JVM truststore
Spring Cloud Bindings Enabled
Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -agentpath:/layers/paketo-buildpacks_bellsoft-liberica/jvmkill/jvmkill-1.16.0-RELEASE.so=printHeapHistogram=1 -XX:ActiveProcessorCount=8 -XX:MaxDirectMemorySize=10M -Xmx385788K -XX:MaxMetaspaceSize=150787K -XX:ReservedCodeCacheSize=240M -Xss1M -Dorg.springframework.cloud.bindings.boot.enable=true

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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: default,test

Viewing the active profile with spring actuator

If you are able to access the the spring actuator, test profile has full unauthenticated access, you can also see which profiles are currently active by querying /actuator/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 four files:

├── application-test.properties
├── application-dev.properties
├── application.properties
├── application-default.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 that 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

Very similar to the test profile but with some differences that include a different persistence strategy (update) which attempts to automatically migrate your database upon each application restart.

application.properties - production

Any configuration values that need to be applied within a production environment should be applied here, this profile is where security is considered most seriously.

This profile doesn’t need to be set, it will be applied given the absence of the any other profile.

application-default.properties

This file is technically another profile called default. It is loaded before all other profiles and contains all global configuration values that are consistent across all profiles. Given that profiles are additive and this profile is loaded first, any values that require overriding can be done in the the selected profile.

For example, for the value of the property spring.datasource.tomcat.max-wait that is defined in the application-default.propertiesfile , to override it in our dev profile, we can add it to application-dev.properties. The value that is defined in the application-dev.properties takes presendance.

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.

Start modelling your app today.