Stage switch with Spring


Posted by Steven

Like other applications, my main project runs through several quality gates before being deployed on the production stage. For example, there is a developer stage, which simply is the locally running version on every developer machine, and a test stage. This test stage is similar to the production setup and can be used to test features before they are merged into the main branch of your version control system. Many projects use an additional quality stage which runs the latest stable build so the customer can test, too. The deployed system behaves differently for every stage. The simplest difference is the database used. You don't want to use the same database for your production and test system, so the deployed code has to decide which database to use. This article describes, how we use Spring to setup different stages.

Because the deployed code is the same in every stage, we need a system property to set the stage. In this example, there are only two instances, using the following system properties:

  1. developer stage: no system property set (to keep the costs for setting up a new developer machine as low as possible)
  2. test stage: spring.profiles.active = test

As mentioned above, other stages can easily be added to this setup.

This system property has to be used by Java code. To implement clean code, the first step is to have an enum for the system properties used:

  1. package constants;
  2.  
  3. public interface Constants {
  4.  
  5. // active Spring profile
  6. String KEY_SPRING_PROFILE = "spring.profiles.active";
  7. }

Depending on the stage the application is running on, there are different configurations to use. These are listed in properties files. For each stage, there is one property file. They have the same attributes, but different values. To keep this example simple, there are just two attributes in the files. One for the name of the application (which is the same regardless of the stage) and one string that represents the stage itself. This is the properties file for the local development stage:

  1. # Client Properties for local environment
  2.  
  3. web.app.name=/MyProject/
  4.  
  5. stage=developer-instance

This is the properties for the test stage:

  1. # Client Properties for test environment
  2.  
  3. web.app.name=/MyProject/
  4.  
  5. stage=test-instance

Using Java configuration, these configuration files can be mapped to a Java class:

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.PropertySource;
  4.  
  5. import static constants.Constants.KEY_SPRING_PROFILE;
  6.  
  7. /**
  8.  * This class is a Java-mapping of a properties file. Every attribute in the file
  9.  * is an attribute in this class. Which property-file is mapped is decided on the
  10.  * configuration in the PropertySource-annotation. If a system property
  11.  * "spring.profiles.active" is set, it is used to determine the path of the property
  12.  * file. For the test stage, this would be "test/client.properties".
  13.  * If no system property is set, "developer/client.properties" will be mapped.
  14.  */
  15. @PropertySource("classpath:${"+KEY_SPRING_PROFILE+":developer}/client.properties")
  16. @Configuration
  17. public class Properties
  18. {
  19. @Value("${web.app.name}")
  20. private String webAppName;
  21.  
  22. @Value("${stage}")
  23. private String stage;
  24.  
  25. public String getWebAppName() {
  26. return webAppName;
  27. }
  28.  
  29. public String getStage() {
  30. return stage;
  31. }
  32. }

There are two things happening in this class. First, the annotation of the class decides which property file to map. If there is a system property "spring.profiles.active", it is used. If there is no property, the default "developer" is used. The path of the property file is "[stage]/client.properties".

Second, each attribute of the property file is mapped into the Java class to be used by the application.

Of course, this setup has to be tested. Here's the test for the locally running developer stage:

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.test.annotation.DirtiesContext;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8.  
  9. import static org.junit.Assert.assertEquals;
  10.  
  11. /**
  12.  * Test for the local developer stage that doesn't use any system properties.
  13.  */
  14. @ContextConfiguration(classes = {Config.class})
  15. @RunWith(SpringJUnit4ClassRunner.class)
  16. @DirtiesContext
  17. public class DeveloperInstanceTest {
  18.  
  19. @Autowired
  20. private ApplicationContext applicationContext;
  21.  
  22. @Test
  23. public void propertyTest() {
  24. Properties properties = applicationContext.getBean(Properties.class);
  25. assertEquals("developer-instance", properties.getStage());
  26. }
  27. }

Here's the slightly more complicated version for the test stage. ProvideSystemProperty is used to set the system property "spring.profiles.active" to "test":

  1. import constants.Constants;
  2. import org.junit.ClassRule;
  3. import org.junit.Test;
  4. import org.junit.contrib.java.lang.system.ProvideSystemProperty;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.test.annotation.DirtiesContext;
  9. import org.springframework.test.context.ContextConfiguration;
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  11.  
  12. import static org.junit.Assert.assertEquals;
  13.  
  14. /**
  15.  * Test for the test stage that uses a system property spring.profiles.active=test.
  16.  */
  17. @ContextConfiguration(classes = {Config.class})
  18. @RunWith(SpringJUnit4ClassRunner.class)
  19. @DirtiesContext
  20. public class TestInstanceTest {
  21.  
  22. // This has to be a ClassRule to set the property before the Spring setup started. Rule is not sufficient here.
  23. @ClassRule
  24. public static final ProvideSystemProperty provideSystemProperty = new ProvideSystemProperty(Constants.KEY_SPRING_PROFILE, "test");
  25.  
  26. @Autowired
  27. private ApplicationContext applicationContext;
  28.  
  29. @Test
  30. public void propertyTest() {
  31. Properties properties = applicationContext.getBean(Properties.class);
  32. assertEquals("test-instance", properties.getStage());
  33. }
  34. }

The complete code can be found here.

TL;DR

Use Java configuration and Spring to map the stage your application is running on into your code.

Share: