Spring Profiles are a powerful feature of the Spring Framework that allows developers to segregate parts of their application configuration and make it available only in certain environments. This is particularly useful for managing different configurations for development, testing, and production environments without having to maintain multiple separate configuration files.
In this tutorial, we will explore how to use Spring Profiles effectively in a Spring Boot application. We'll cover the basics of defining profiles, activating them, and using them to manage environment-specific configurations.
Spring Profiles allow you to define beans that should only be loaded when a specific profile is active. This means you can have different sets of beans for different environments, such as development, testing, and production.
You can define properties specific to a profile in application-{profile}.properties or application-{profile}.yml files. For example:
server.port=8080
app.name=MyApp-Dev
server.port=8081
app.name=MyApp-Prod
You can also define beans that should only be loaded for specific profiles using the @Profile annotation.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("dev")
public String devBean() {
return "This is a development bean";
}
@Bean
@Profile("prod")
public String prodBean() {
return "This is a production bean";
}
}
You can activate profiles by setting the spring.profiles.active property in your main application.properties or application.yml file.
spring.profiles.active=dev
You can also activate profiles when starting your Spring Boot application using command-line arguments:
java -jar myapp.jar --spring.profiles.active=prod
Another way to activate profiles is by setting the SPRING_PROFILES_ACTIVE environment variable.
export SPRING_PROFILES_ACTIVE=dev
java -jar myapp.jar
Let's walk through a real-world example where we use Spring Profiles to manage database configurations for development and production environments.
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_password
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_password
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Configuration
public class DataSourceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/dev_db");
dataSource.setUsername("dev_user");
dataSource.setPassword("dev_password");
return dataSource;
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://prod-db-server:3306/prod_db");
dataSource.setUsername("prod_user");
dataSource.setPassword("prod_password");
return dataSource;
}
}
You can activate the profile when running your application:
java -jar myapp.jar --spring.profiles.active=dev
Spring Profiles provide a flexible and powerful way to manage environment-specific configurations in a Spring Boot application. By using profiles, you can keep your configuration organized, secure, and optimized for different environments. This tutorial has covered the basics of defining and activating profiles, as well as best practices for their use.
By following these guidelines and examples, you should be able to effectively manage your Spring Boot application's configurations across multiple environments.