Skip to main content

Setting up Dev and Test Profiles with Dummy Data

Overview

In this lab, you will:

  • Create two Spring Boot profiles: dev and test.
  • Set different property values for each profile.
  • Use these properties to print different dummy data depending on the active profile.

You will work inside your Application class to log or print the environment-specific information.

Project Setup

  • Group: com.neueda
  • Artifact: profile-demo
  • Name: profile-demo
  • Dependencies:
    • Spring Web
    • Spring Boot DevTools (optional, for easy restart)

Step 1: Create Profile-Specific Property Files

In src/main/resources, create:

1. application-dev.properties

Insert this into the file:

Click here to view the example solution
myapp.environment=Development Environment
myapp.dummydata=Dummy Dev Data Loaded

2. application-test.properties

Click here to view the example solution
myapp.environment=Testing Environment
myapp.dummydata=Dummy Test Data Loaded

Note: There should already be a default application.properties file. Leave it mostly empty or use it for shared settings.

Step 2: Update the Main Application Class

  • File: src/main/java/com/neueda/profile_demo/ProfileDemoApplication.java
  • Inject the properties to be read from application-<profile>.properties using @Value.
  • Log the values depending on the active profile.

Example structure:

Click here to view the example solution
@SpringBootApplication
public class ProfileDemoApplication {

private static final Logger logger = LoggerFactory.getLogger(ProfileDemoApplication.class);

@Value("${myapp.environment:dev}")
private String environment;

@Value("${myapp.dummydata:Dummy Dev Data Loaded by default}")
private String dummyData;

public static void main(String[] args) {
SpringApplication.run(ProfileDemoApplication.class, args);
}

@Bean
public String environmentInfo() {
logger.info("****Application is running in {} environment****", environment);
logger.info("****Dummy data: {}****", dummyData);
return "Environment: " + environment + ", Dummy Data: " + dummyData;
}
}

Step 3: Running and Testing

  • Make sure your application.properties has one of these lines:
Click here to view the example solution
spring.profiles.active=dev
  • Start your application. You should see logs showing Development Environment and Dummy Dev Data Loaded.

  • Now change it to:

Click here to view the example solution
spring.profiles.active=test
  • Restart the app. You should see logs showing Testing Environment and Dummy Test Data Loaded.

Note:

  • It is also possible to set the profile whilst running using mvn
  • Remove the spring.profiles.active line from application-properties
  • For mvn run:
Click here to view the example solution
mvn spring-boot:run -Dspring-boot.run.profiles=dev
  • The dev properties should be logged as expected
  • For gradle run (either of these commands):
Click here to view the example solution
./gradlew bootRun --args='--spring.profiles.active=dev'
./gradlew bootRun -Dspring.profiles.active=dev

Step 4 (Extension Task): Create a Profile-Based Configuration Class

  • Create a configuration class to load profile based values.

Example properties:

Click here to view the example solution
# application-dev.properties
name=Developer Name
version=1.0-DEV

# application-test.properties
name=Test Developer Name
version=1.0-DEV-TEST
  • Create a class called ProfileConfig
  • Annotate the class with @Component and @ConfigurationProperties
  • Create an instance variable for name and version of type string
  • Add the relevant getters and setters
  • Inject and use the ProfileConfig class in the main ProfileDemoApplication class (annotate with autowired)
  • Try switching profiles using the command line when you run the application to see the output logged change

ProfileConfig Class

Click here to view the example solution
@ConfigurationProperties
@Component
public class ProfileConfig {

private String name;
private String version;

// Getters and setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}
}

Updated Main Application Class (Option 1)

Click here to view the example solution
@SpringBootApplication
public class ProfileDemoApplication {

private static final Logger logger = LoggerFactory.getLogger(ProfileDemoApplication.class);

@Autowired
private ProfileConfig profileConfig;

public static void main(String[] args) {
SpringApplication.run(ProfileDemoApplication.class, args);
}

@Bean
public String environmentInfo() {
logger.info("Profile Name: {}", profileConfig.getName());
logger.info("Profile Version: {}", profileConfig.getVersion());
return "Profile Name: " + profileConfig.getName() + ", Profile Version: " + profileConfig.getVersion();
}
}

Updated Main Application Class (Option 2 - Combined Approach)

This method will also work with the previous format for reading values from the properties files using a specific prefix:

Click here to view the example solution
@SpringBootApplication
public class ProfileDemoApplication {

private static final Logger logger = LoggerFactory.getLogger(ProfileDemoApplication.class);

@Value("${myapp.environment:dev}")
private String environment;

@Value("${myapp.dummydata:Dummy Dev Data Loaded by default}")
private String dummyData;

@Autowired
private ProfileConfig profileConfig;

public static void main(String[] args) {
SpringApplication.run(ProfileDemoApplication.class, args);
}

@Bean
public String environmentInfo() {
logger.info("Environment: {}", environment);
logger.info("Dummy Data: {}", dummyData);
logger.info("Profile Name: {}", profileConfig.getName());
logger.info("Profile Version: {}", profileConfig.getVersion());
return "Environment: " + environment + ", Dummy Data: " + dummyData + ", Profile Name: " + profileConfig.getName() + ", Profile Version: " + profileConfig.getVersion();
}
}

Now both the values beginning myapp. will be logged along with the name and version from the relevant properties file.