# Spring Boot How to Set Active Profile
## Introduction
In Spring Boot, setting the active profile determines which configuration properties are loaded and which beans are instantiated. This is particularly useful when an application moves through various stages such as development, testing, staging, and production, each requiring different configurations. Managing profiles effectively helps in sandboxing configurations and behaviors specific to various environments. Without proper use of profiles, manually changing configurations for each environment can be cumbersome and error-prone, leading to inconsistent behavior and deployment issues.
## Solution: Setting Active Profile in Spring Boot
There are multiple ways to set the active profile in a Spring Boot application, catering to different scenarios and preferences. Below are some common methods, demonstrated with Kotlin sample code.
### 1. Via `application.properties` or `application.yml`
Add the `spring.profiles.active` property in your `application.properties` or `application.yml` file.
**application.properties:**
```properties
spring.profiles.active=development
application.yml:
spring:
profiles:
active: development
2. Command Line Argument
You can set the active profile via command line when running your Spring Boot application.
$ java -jar myapp.jar --spring.profiles.active=development
3. Environment Variable
You can also set the active profile using an environment variable.
$ export SPRING_PROFILES_ACTIVE=development
4. Programmatically
You can set the active profile programmatically within your Spring Boot application.
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class MySpringBootApplication
fun main(args: Array<String>) {
val app = SpringApplication(MySpringBootApplication::class.java)
app.setAdditionalProfiles("development")
app.run(*args)
}
Profile-Specific Configuration Files
Spring Boot allows you to have profile-specific configuration files like application-{profile}.properties
or application-{profile}.yml
. These files will be loaded only if the respective profile is active.
application-development.properties:
logging.level.root=DEBUG
application-production.properties:
logging.level.root=WARN
logging.file=/var/log/myapp.log
Sample Code Demonstration
For instance, let’s create a simple Spring Boot application with multiple profiles.
Directory Structure:
src/
└── main/
├── java/
│ └── com/
│ └── example/
│ └── demo/
│ ├── DemoApplication.kt
│ └── config/
│ └── DatabaseConfig.kt
└── resources/
├── application.properties
├── application-development.properties
└── application-production.properties
DemoApplication.kt:
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
DatabaseConfig.kt:
package com.example.demo.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import javax.sql.DataSource
import org.apache.commons.dbcp2.BasicDataSource
@Configuration
class DatabaseConfig {
@Profile("development")
@Bean
fun developmentDataSource(): DataSource {
val dataSource = BasicDataSource()
dataSource.url = "jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1"
dataSource.username = "sa"
dataSource.password = "password"
return dataSource
}
@Profile("production")
@Bean
fun productionDataSource(): DataSource {
val dataSource = BasicDataSource()
dataSource.url = "jdbc:mysql://prodDb:3306/prodDb"
dataSource.username = "prodUser"
dataSource.password = "prodPassword"
return dataSource
}
}
Running the Application
When you run the application with a specific profile:
$ java -jar myapp.jar --spring.profiles.active=development
Output:
2023-10-15 12:00:00.000 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.234 seconds
2023-10-15 12:00:01.000 DEBUG 2 --- [ main] com.example.demo.config.DatabaseConfig : Using in-memory database for development profile
Switching to the production profile:
$ java -jar myapp.jar --spring.profiles.active=production
Output:
2023-10-15 12:00:02.000 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.234 seconds
2023-10-15 12:00:03.000 INFO 2 --- [ main] com.example.demo.config.DatabaseConfig : Connected to MySQL production database
Summary
Setting the active profile in Spring Boot is a powerful feature that helps in managing different configurations for different environments, streamlining the deployment process, and ensuring consistent behavior. You can set the active profile via various methods including configuration files, command line arguments, environment variables, and programmatically. Profile-specific configuration files further enhance this capability by enabling fine-grained control over the application’s behavior across different environments.
Meta:Similar
- Spring Boot application configuration
- Spring Boot profile-specific beans
- Using @Profile annotation in Spring Boot
- Spring Boot environment-based configuration
- Managing multiple data sources in Spring Boot
- Spring Boot YAML configuration
- Spring Boot externalized configuration
- Spring Boot configuration properties
- Spring Boot command-line arguments
- Spring Boot environment variables
[…] common task that developers frequently encounter. This conversion, known as “string to int Java,” is crucial for applications that require numerical computations from user input or text […]