Profiles in Springboot
Profiles in Springboot
Application configuration in Spring Boot is all about managing different settings and
behaviors that your application needs in different environments, such as development, testing,
and production. This configuration management includes setting up database URLs, cache
settings, logging levels, API keys, and more, which often vary depending on where the
application is running. Spring Boot makes this process easier by letting you define multiple
configurations in a single file (e.g., application.yml), with the ability to activate them
selectively using profiles.
When you dive into application configuration, you’ll learn how to:
Profiles let you define different configurations for different environments within the same
application. With Spring Boot profiles, you set up multiple configurations that automatically
adjust based on the profile you activate.
Abilash Shankarpalli
4. Flexible Behavior Management: You can use profiles to control which beans,
services, or configurations to load, making your app adaptable.
# application.yml
# Default configuration
spring:
datasource:
url: jdbc:h2:mem:defaultdb
driver-class-name: org.h2.Driver
username: default_user
password: default_pass
application:
name: MyApp
# Development configuration
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:mysql://localhost:3306/dev_db
driver-class-name: com.mysql.cj.jdbc.Driver
username: dev_user
password: dev_pass
application:
name: MyApp (Development)
# Production configuration
---
spring:
config:
activate:
on-profile: prod
datasource:
Abilash Shankarpalli
url: jdbc:mysql://localhost:3306/prod_db
driver-class-name: com.mysql.cj.jdbc.Driver
username: prod_user
password: prod_pass
application:
name: MyApp (Production)
@Configuration
public class DataSourceConfig {
@Bean
@Profile("dev")
public DataSource devDataSource() {
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/dev_db")
.username("dev_user")
.password("dev_pass")
.build();
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/prod_db")
.username("prod_user")
.password("prod_pass")
.build();
}
}
Gradle allows you to activate profiles by passing arguments or by specifying them within the
Gradle tasks.
To specify a profile when you run the application, you can pass it as a command-line
argument to Gradle:
This command activates the specified profile, and Spring Boot will load the corresponding
properties from application.yml.
Abilash Shankarpalli
You can define custom Gradle tasks that set profiles automatically. For instance:
// build.gradle
tasks.register("bootRunDev",
org.springframework.boot.gradle.tasks.run.BootRun) {
group = "application"
description = "Run the application with the dev profile"
args = ["--spring.profiles.active=dev"]
}
tasks.register("bootRunProd",
org.springframework.boot.gradle.tasks.run.BootRun) {
group = "application"
description = "Run the application with the prod profile"
args = ["--spring.profiles.active=prod"]
}
Using @Value
If you want to inject specific properties from a profile, you can use the @Value annotation:
@Component
public class ApplicationProperties {
@Value("${spring.datasource.url}")
private String dataSourceUrl;
@Value("${spring.datasource.username}")
private String username;
Using @ConfigurationProperties
@Component
Abilash Shankarpalli
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
spring:
datasource:
url: jdbc:mysql://localhost:3306/dev_db
username: dev_user
password: dev_pass
@Service
@Profile("dev")
public class DevEmailService implements EmailService {
@Override
public void sendEmail(String to, String message) {
System.out.println("Sending email in development mode to " + to);
}
}
@Service
@Profile("prod")
public class ProdEmailService implements EmailService {
@Override
public void sendEmail(String to, String message) {
// Actual production email service code
}
}
This setup will use DevEmailService when the development profile is active, and
ProdEmailService for production, allowing different behavior based on the environment.
When testing, you can specify which profile should be active using @ActiveProfiles:
Abilash Shankarpalli
@SpringBootTest
@ActiveProfiles("test")
public class MyServiceTest {
@Autowired
private DataSource dataSource;
@Test
public void testDataSource() {
System.out.println(dataSource.getConnection().getMetaData().getURL());
// Expected to use the test profile datasource URL
}
}
This is useful to isolate configurations during testing, making sure the actual production or
development configurations are not affected.
For production deployments, sensitive configuration data (e.g., database passwords) can be
managed externally instead of within application.yml.
By pointing Spring Boot to an external configuration file, you keep sensitive information
separate from your codebase.
7. Best Practices for Managing Profiles with Gradle and Spring Boot
Abilash Shankarpalli
Feel free to reach out or mail me at
[email protected], for help with
Interview preparation or resume reviews.
Happy Learning!
Abilash Shankarpalli