One of the most common problems while we are developing applications is to have just one code and execute it in different environments. In this post we will explain how to support multiple environments using Spring boot step by step, to do it we will start with the project created in the post Spring boot + Jersey.
First steps
Before to continue learning how to configure the Spring profiles, we have to understand the following points:
- For each environment we will have a profile, therefore if we have the environments Development, QA and Production we will have 3 profiles in our application.
- In Spring boot the profiles are defined on runtime it means that you don’t need to compile your code by using an specific flag.
- There are many ways to define a profile in Spring boot, in this post we will explain 2 by using properties and yaml files.
Defining profiles by using properties files
To configure the Spring profiles we have to execute the following steps:
1 Add a properties file per profile
The first step will be to create a properties file in the /src/main/resources folder for each environment that we will support by using the following structure:
application-${profile}.properties
${profile} Represents the environment, for this example we will create the following 3 files:
- application-dev.properties
com.raidentrance.app.name=Spring boot dev
- application-qa.properties
com.raidentrance.app.name=Spring boot qa
- application-prod.properties
com.raidentrance.app.name=Spring boot prod
2 Create a new service to show the value of the property
Once we create the property named com.raidentrance.app.name the next step will be present its value in a web service, to do it we will use the following service:
/** * */ package com.raidentrance.resource; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author maagapi * */ @Component @Path("/configs") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class ProjectConfigurationResource { @Value("${com.raidentrance.app.name}") private String appName; private static final Logger log = LoggerFactory.getLogger(UserResource.class); @GET @Path("/appName") public Response getAppName() { log.info("Getting project configuration"); return Response.ok(appName).build(); } }
The previous service will show in the response of the endpoint GET /configs/appName the value defined in our properties file
3 Register the service in Jersey
We have to remember that if we create a new service we need to include it in the JerseyConfig class.
/** * */ package com.raidentrance.config; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; import com.raidentrance.resource.ProjectConfigurationResource; import com.raidentrance.resource.UserResource; /** * @author raidentrance * */ @Component public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(UserResource.class); register(ProjectConfigurationResource.class); } }
The UserResource was created in the previous example and it is not necessary for this example.
4 Testing everything together
To test it is necessary to compile our application by using the command mvn clean install as you can see it is not necessary to include the profile by compiling, what it is going to be different is the execution, to execute our application we will execute the command:
mvn -Dspring.profiles.active=qa spring-boot:run
As you can see the previous command will use the configurations defined in the qa profile.
If we execute the endpoint GET /configs/appName we will get the following result:
Spring boot qa
Defining profiles by using a yaml file
Once we know how to define the profiles by using multiple .properties file the next step will be to understand how to do it by suing yaml files.
To support multiple profiles by using yaml files we only need to modify the step number 1.
1 Define the configurations
When we create the configurations in a yaml file we don’t need to create multiple files per environment, we only need to create one, lets see the structure:
spring: profiles: active: dev --- spring: profiles: dev com: raidentrance: app: name: Spring boot dev --- spring: profiles: qa com: raidentrance: app: name: Spring boot qa --- spring: profiles: prod com: raidentrance: app: name: Spring boot prod
As you can observe now you can define in the file the default profile and the properties for all the environments.
You can find the code in the following url https://github.com/raidentrance/spring-boot-example/tree/part7-profiles.
Don’t forget to follow us in our social networks in https://twitter.com/geeks_mx and in https://www.facebook.com/geeksJavaMexico/.
Autor: Alejandro Agapito Bautista
Twitter: @raidentrance
Contacto:raidentrance@gmail.com