For the complete documentation index, see llms.txt. This page is also available as Markdown.

Spring profiles

As Java projects pass from development into production it is sometimes necessary to use different suites of project dependencies and settings, loaded as a profile. In a typical Spring project this can be achieved following a project structure such as:

A class service.LoaderService

package com.diffblue.service;
import com.diffblue.loader.Loader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
// Simple service implementation
@Service
public class LoaderService {
  private final Loader loader;
  @Autowired
  public LoaderService(Loader loader) {
    this.loader = loader;
  }
  public String getMessage() {
    return "service + " + loader.getMessage();
  }
}

has an instance of an interface loader.Loader.

Another class loader.MyLoader implements that interface,

which is used to supply the loader.Loader interface.

This is done in a class config.ProductionConfig where the user has the possibility to configure the loader by using the @Configuration and @Profile() annotations:

The class config.ProductionConfig should be annotated with @Profile("!test-scenario") in production mode:

When using IntelliJ, the Spring profile in use must be declared in Run > Edit Configurations > VM Options:

In development mode, the annotation @Profile("test-scenario"), should be used in a class config.TestConfig placed in the test directory:

which configures a test loader through the loader.TestLoader also placed in the test directory

These two classes are placed in the test directory in order to be isolated from a run in the production mode.

To use a specific profile in development mode, the --active-profiles option must be used:

Last updated

Was this helpful?