The post How to read different properties file based on Spring Profile in a Spring MVC project appeared first on Little Big Extra.
]]>This tutorial aims towards providing a solution for selecting properties file based on the profile injected for a Spring MVC application at run time.
Technologies used
Spring profiles can be passed as an environment variable like
-DSPRING_PROFILES_ACTIVE="dev"where dev is the profile name
If you have set the profile on the server and want it to retrieve it within your application you can use System.getProperty or System.getenv methods.
Here is the code which fetches the profile and defaults it to a local profile, if no profile has been found.
private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE"; String profile; /** * In local system getProperty() returns the profile correctly, however in docker getenv() return profile correctly * */ protected void setSpringProfile(ServletContext servletContext) { if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){ profile=System.getenv(SPRING_PROFILES_ACTIVE); }else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){ profile=System.getProperty(SPRING_PROFILES_ACTIVE); }else{ profile="local"; } log.info("***** Profile configured is ****** "+ profile); servletContext.setInitParameter("spring.profiles.active", profile); }
Also if you are using Java configuration for spring that is, extending a class from WebApplicationInitializer you need to set the profile in servlet context by using setinitParamter() method which has been added as the last line in above method.
servletContext.setInitParameter("spring.profiles.active", profile);
If you have come this far without any problems then the next step is to create a different properties file for different environments.
You might want to create different properties files in the format like application-{profile}.properties, so you might end up creating something like this.
To access the application-dev.properties, say now you will need to use
@Profile("dev")at the class level
The following code will fetch the application-dev.properties and common.properties
@Configuration @Profile("dev") public class DevPropertyReader { @Bean public static PropertyPlaceholderConfigurer properties() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[] { new ClassPathResource("properties/common.properties"), new ClassPathResource("properties/application-dev.properties") }; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; } }
For accessing say application-prod.properties you have to use
@Profile("prod")at class level
@Configuration @Profile("prod") public class ProdPropertyReader { @Bean public static PropertyPlaceholderConfigurer properties() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[] { new ClassPathResource("properties/common.properties"), new ClassPathResource("properties/application-prod.properties") }; ppc.setLocations(resources); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; } }
To access the properties in controller, all you have to do is to use the @Value annotation
@Value("${application.message}") String message;
Hope,it helps !!
The post How to read different properties file based on Spring Profile in a Spring MVC project appeared first on Little Big Extra.
]]>The post How to use Spring Profiles with Docker Containers appeared first on Little Big Extra.
]]>
Spring Profiles are an effective way of implementing environment independent code. The properties file or @Beans can be selected dynamically at run time based on the profile injected.
Assuming that you are quite familiar with the spring profiles and looking for injecting profiles in a Docker environment. There are couple of ways of doing it namely
In this tutorial, I will try to capture all these 3 scenarios.
Read Here: How to create Docker image of Standalone Spring MVC project
From command prompt of your system, any spring boot application can be run with “java -jar” command.The profiles need to be passed as an argument like this “-Dspring.profiles.active=dev“. For Spring MVC applications other 2 below methods will work fine.
java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=dev -jar rest-api.jar
Similarly, when using dockerfile we need to pass the profile as an argument, have a look at one of the Dockerfile for creating a spring boot docker image
Below an example on spring boot project dockerfile
FROM java:8 ADD target/my-api.jar rest-api.jar RUN bash -c 'touch /pegasus.jar' ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=dev","-jar","/rest-api.jar"]
Pay attention to the last line ENTRYPOINT, in this line we are passing the java command to execute the jar file and all arguments have been passed as comma separated values. “-Dspring.profiles.active=dev” is where we are passing the dev profile, you can replace dev with the profile name you want.
You can also pass spring profile as an environment variable while using docker run command using the -e flag. The option -e “SPRING_PROFILES_ACTIVE=dev” will inject the dev profile to the Docker container.
docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:latest
If you are on DockerSwarm or using compose file for deploying docker images the Spring profile can be passed using the environment: tag in a docker-compose file, as shown below
version: "3" services: rest-api: image: rest-api:0.0.1 ports: - "8080:8080" environment: - "SPRING_PROFILES_ACTIVE=dev"
The post How to use Spring Profiles with Docker Containers appeared first on Little Big Extra.
]]>