Spring Boot Profiles provide a way to segregate parts of your application configuration and make it available only in certain environments. This is particularly useful for managing different configurations for development, testing, and production environments.

Key Concepts

  1. Profiles: Logical grouping of configuration settings.
  2. Application Properties: Different property files for different profiles.
  3. Activating Profiles: Methods to activate a specific profile.
  4. Profile-Specific Beans: Defining beans that are only available in certain profiles.

Application Properties

Spring Boot allows you to define multiple property files for different profiles. The default property file is application.properties or application.yml. For profile-specific configurations, you can create files like application-dev.properties, application-test.properties, and application-prod.properties.

Example

application.properties

spring.application.name=MyApp
server.port=8080

application-dev.properties

server.port=8081
spring.datasource.url=jdbc:h2:mem:devdb
spring.datasource.username=devuser
spring.datasource.password=devpass

application-prod.properties

server.port=8082
spring.datasource.url=jdbc:mysql://prod-db-server/mydb
spring.datasource.username=produser
spring.datasource.password=prodpass

Activating Profiles

You can activate a profile in several ways:

  1. Command Line: Use the --spring.profiles.active argument.

    java -jar myapp.jar --spring.profiles.active=dev
    
  2. Environment Variable: Set the SPRING_PROFILES_ACTIVE environment variable.

    export SPRING_PROFILES_ACTIVE=dev
    
  3. Application Properties: Set the active profile in application.properties.

    spring.profiles.active=dev
    
  4. Programmatically: Activate profiles in your main application class.

    @SpringBootApplication
    public class MyApp {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(MyApp.class);
            app.setAdditionalProfiles("dev");
            app.run(args);
        }
    }
    

Profile-Specific Beans

You can define beans that are only available in certain profiles using the @Profile annotation.

Example

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        return new HikariDataSource();
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        return new HikariDataSource();
    }
}

Practical Exercise

Task

  1. Create a Spring Boot application with two profiles: dev and prod.
  2. Define different server.port values for each profile.
  3. Create a DataSource bean that is specific to each profile.

Solution

  1. Create the Project Structure:

    • src/main/resources/application.properties
    • src/main/resources/application-dev.properties
    • src/main/resources/application-prod.properties
    • src/main/java/com/example/demo/DemoApplication.java
    • src/main/java/com/example/demo/DataSourceConfig.java
  2. Define Properties:

application.properties

spring.application.name=ProfileDemo

application-dev.properties

server.port=8081
spring.datasource.url=jdbc:h2:mem:devdb
spring.datasource.username=devuser
spring.datasource.password=devpass

application-prod.properties

server.port=8082
spring.datasource.url=jdbc:mysql://prod-db-server/mydb
spring.datasource.username=produser
spring.datasource.password=prodpass
  1. Main Application Class:

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. DataSource Configuration:

DataSourceConfig.java

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:h2:mem:devdb");
        dataSource.setUsername("devuser");
        dataSource.setPassword("devpass");
        return dataSource;
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://prod-db-server/mydb");
        dataSource.setUsername("produser");
        dataSource.setPassword("prodpass");
        return dataSource;
    }
}

Common Mistakes and Tips

  1. Forgetting to Activate Profiles: Ensure you activate the correct profile when running your application.
  2. Profile-Specific Beans Not Loaded: Make sure the @Profile annotation matches the active profile.
  3. Property File Naming: Ensure profile-specific property files follow the application-{profile}.properties naming convention.

Conclusion

Spring Boot Profiles are a powerful feature for managing different configurations for various environments. By understanding how to define and activate profiles, you can create flexible and maintainable applications that adapt to different deployment scenarios. In the next module, we will explore how to use Docker with Spring Boot to further enhance your application's deployment capabilities.

Spring Boot Course

Module 1: Introduction to Spring Boot

Module 2: Spring Boot Basics

Module 3: Building RESTful Web Services

Module 4: Data Access with Spring Boot

Module 5: Spring Boot Security

Module 6: Testing in Spring Boot

Module 7: Advanced Spring Boot Features

Module 8: Deploying Spring Boot Applications

Module 9: Performance and Monitoring

Module 10: Best Practices and Tips

© Copyright 2024. All rights reserved