Skip to main content
Chapter 17 of 18 · 1 sections

Spring: Profiles and Environments

Select environment-specific configuration and activate profiles in applications and tests.

Original chapter: Spring profile. The text and examples below are preserved from the historical source; read the corrections above before using them.

Section 17.1: Spring Profiles allows to configure parts available for certain environment

Any @Component or @Configuration could be marked with @Profile annotation

Source example · PDF page 64
@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...
}

The same in XML config

Source example · PDF page 64
<beans profile="dev">
   <bean id="dataSource" class="<some data source class>" />
</beans>

Active profiles could be configured in the application.properties file

Source example · PDF page 64
spring.profiles.active=dev,production

or specified from command line

Source example · PDF page 64
--spring.profiles.active=dev,hsqldb

or in SpringBoot

Source example · PDF page 64
SpringApplication.setAdditionalProfiles("dev");

It is possible to enable profiles in Tests using the annotation @ActiveProfiles("dev")

Buy me a coffee
Daily Practice
0/0