0 day streak
java / beginner
Snippet

Environment-Specific Beans with @Profile

The @Profile annotation allows you to restrict the loading of beans to specific environments, such as 'development', 'test', or 'production'. This helps in managing different configurations easily.

snippet.java
java
1
2
3
4
5
6
7
@Component
@Profile("development")
public class DevDataSource {
public DevDataSource() {
System.out.println("Dev Database initialized.");
}
}
spring
Breakdown
1
@Profile("development")
Specifies that this component should only be created if the 'development' profile is active.
2
public class DevDataSource
A class representing a resource that should only exist in a specific environment.