Annotation @Profile
The annotation @Profile
to only enable the Bean
for that specific spring profile.
For example:
@Getter
@Setter
public class DataSource {
private String url;
}
@Configuration
public class MyConfiguration {
@Bean
@Profile("!prod")
public DataSource nonProdDataSource() {
DataSource dataSource = new DataSource();
dataSource.setUrl("sit url");
return dataSource;
}
@Bean
@Profile("prod")
public DataSource prodDataSource() {
DataSource dataSource = new DataSource();
dataSource.setUrl("prod url");
return dataSource;
}
}
As a result, when setting spring.profiles.active: prod
(see Spring Property Profile), only the datasource with prod url
is enabled.