Spring Profiles

A Spring application can be configured to behave differently in different environments using different profiles. For example, you can register beans based on what kind of database you’re running (development, production, test).

You can then configure a deployment to run using zero or more profiles, by providing a spring.profiles.active property. For example, to run the application using production and dev profile, add the following line in application.properties:

spring.profiles.active: dev

To test it, let’s create two classes responsible for database datasource setup and then run our application with datasource configuration setup based on active profile.

@Component
@Profile("dev")
public class DevDatasourceConfig implements DatasourceConfig {

    @Override
    public void setup() {
        System.out.println("Datasource config based on development profile");
    }
}
@Component
@Profile("production")
public class ProductionDatasourceConfig implements DatasourceConfig {

    @Override
    public void setup() {
        System.out.println("Datasource config based on production profile");
    }
}

To inject our DatasourceConfig interface; depending on the active profile we need to create a ‘test’ class. Spring will injects DevDatasourceConfig or ProductionDatasourceConfig bean:

@RestController
public class DatasourceProfileController {

    @Autowired
    DatasourceConfig datasourceConfig;

    @RequestMapping("/profiles")
    public String profiles() {
        datasourceConfig.setup();
        return "Database setup based on Spring profile configuration";
    }
}

When the “dev” profile is active spring injects DevDatasourceConfig object, and on call of setup() method following is the output:

Datasource config based on development profile

To see a list of active profiles you just need to inject Environment and then iterate through a list of active profiles.

@RestController
public class DatasourceProfileController {

    @Autowired
    DatasourceConfig datasourceConfig;

    @Autowired
    Environment environment;

    @RequestMapping("/profiles")
    public String profiles() {
        datasourceConfig.setup();

        for (final String profileName : environment.getActiveProfiles()) {
            System.out.println("Currently active profile - " + profileName);
        }
        
        return "Database setup based on Spring profile configuration";
    }
}
When you’ll run our app, tou should get a list of active profiles:
Datasource config based on development profile
Currently active profile - dev

Any bean that does not specify a profile belongs to “default” profile.

Spring also provides a way to set the default profile when no other profile is active – by using the “spring.profiles.default” property.

For testing purposes, to specify what profile is active you need to specify an active profile by using @ActiveProfiles annotation:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringForBlogDemoApplication.class })
@ActiveProfiles("dev")
public class DatasourceProfileControllerTest {

    @Autowired
    DatasourceConfig datasourceConfig;

    @Test
    public void test_datasourceSetupBasedOnProfile() {
        datasourceConfig.setup();
    }
}

The profile names can also be passed in via a JVM system parameter. The profile names passed as the parameter will be activated during application start-up:

-Dspring.profiles.active=dev

Leave a Reply