Configure different spring profiles in spring boot application

Configure different spring profiles in spring boot application

Managing Spring Boot profiles for different environments along with Docker

Spring profiles provide a way to create application config for different environments, i.e, dev, qa, stage, production, etc.
In this tutorial, we are going to learn how we can set up the Spring application config for different environments in different ways.

Defining Profiles

We can define the profile in properties, a YAML file, or directly inside the code.

Using Property/YAML Files

We can define the configuration inside a properties file and separate it for different environments. For example, we can create application-{profile}.properties or application-{profile}.yml i.e application-{dev}.yml for development application-{prod}.yml for production.
application-dev.yml (Development specific):
java
server:
  port: 8081
spring:
  datasource:
    url: jdbc:h2:mem:devdb
application-prod.yml (Production specific):
java
spring:
  datasource:
    url: jdbc:mysql://prod-server:3306/proddb

Multi Profile Single File Config

We can also configure all the configs for different environments in a single file. For this spring, provide the three dashes --- to separate the environments.
Inside application.yml:
java
---
spring.config.activate.on-profile: dev
server:
  port: 8081
spring:
  datasource:
    url: jdbc:h2:mem:devdb

---
spring.config.activate.on-profile: prod
spring:
  datasource:
    url: jdbc:mysql://prod-server:3306/proddb

---
spring.config.activate.on-profile: docker
server.port: 8080

In Java Code

We can set the spring profile for specific spring beans or a configuration class to restrict for only the active profile i.e the beans or configuration file will load only the corresponding profile is active. This can be done using @Profile annotation.
For example:
java
@Configuration
@Profile("dev")
public class DevDataConfig {
    
    @Bean
    public DataSource dataSource() {
        // Returns a lightweight development database
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
    }
}

Activating Profiles

We have configured the profiles in different ways; let's look at how to activate these profiles.

Using Command Line

While running our jar file, we can pass the spring profile as
bash
java -jar myapp.jar --spring.profiles.active=prod

Via JVM System Properties

bash
java -Dspring.profiles.active=dev -jar myapp.jar

Running Docker Container

If we are using a Docker container to run the application, we can set the environmental files in the Docker run command. This is very useful if you wanna hide the configuration data in production from development teams for security reasons.
bash
docker run -d \
  --name csbyte \
  -p 8081:8080 \
  -v $(pwd)/application-prod.yml:/app/config/application.yml \
  -e SPRING_PROFILES_ACTIVE=prod
  csbyte
Here, -v $(pwd)/application-prod.yml:/app/config/application.yml This will use the current directory file as the config properties file.
-e SPRING_PROFILES_ACTIVE=prod this will activate the code-based profiling as mentioned earlier i.e @Profile("prod").
By utilizing the profiling in a Spring Boot application, we can develop a highly maintainable, secure application.