Spring MVC – Little Big Extra http://www.littlebigextra.com A technology blog covering topics on Java, Scala, Docker, AWS, BigData, DevOps and much more to come. Do it yourself instructions for complex to simple problems for a novice to an expert. Wed, 28 Nov 2018 22:10:31 +0000 en-US hourly 1 https://wordpress.org/?v=4.8.8 http://www.littlebigextra.com/wp-content/uploads/2023/04/cropped-logo-32x32.png Spring MVC – Little Big Extra http://www.littlebigextra.com 32 32 How to read different properties file based on Spring Profile in a Spring MVC project http://www.littlebigextra.com/how-to-read-different-properties-file-based-on-spring-profile-in-a-spring-mvc-project/ http://www.littlebigextra.com/how-to-read-different-properties-file-based-on-spring-profile-in-a-spring-mvc-project/#respond Mon, 11 Sep 2023 16:26:18 +0000 http://littlebigextra.com/?p=1117 How to read different properties file based on Spring Profile in a Spring MVC project Introduction 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 3.1 Java 8 How to inject Spring profile Spring profiles can be […]

The post How to read different properties file based on Spring Profile in a Spring MVC project appeared first on Little Big Extra.

]]>
Share this article on

How to read different properties file based on Spring Profile in a Spring MVC project

Introduction

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 3.1
  • Java 8

How to inject Spring profile

Spring profiles can be passed as an environment variable like

-DSPRING_PROFILES_ACTIVE="dev"
where dev is the profile name

  • If you are using Eclipse You can add an environment variable say in local tomcat server by doing Right click on Your_Project -> Run AS Config –> Apache tomcat -> VM arguments add -DSPRING_PROFILES_ACTIVE=”dev”, as shown below.
  • If you are using Docker you can have a look at this link.

How to get the Spring Profile within servlet context

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);

 

Picking the right property file based on 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;
    }
}

Access the properties in controller

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.

]]>
http://www.littlebigextra.com/how-to-read-different-properties-file-based-on-spring-profile-in-a-spring-mvc-project/feed/ 0
How to use Spring Profiles with Docker Containers http://www.littlebigextra.com/use-spring-profiles-docker-containers/ http://www.littlebigextra.com/use-spring-profiles-docker-containers/#comments Fri, 11 Aug 2023 10:18:16 +0000 http://littlebigextra.com/?p=1089   How to use Spring Profiles with Docker Containers Introduction 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 […]

The post How to use Spring Profiles with Docker Containers appeared first on Little Big Extra.

]]>
Share this article on

 

How to use Spring Profiles with Docker Containers

Introduction

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

 

 

  • Passing Spring Profile in Dockerfile
  • Passing Spring Profile in Docker run command
  • Passing Spring Profile in DockerCompose

In this tutorial, I will try to capture all these 3 scenarios.

Read Here: How to create Docker image of Standalone Spring MVC project

Passing Spring Profile in a Dockerfile

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.

Passing Spring Profile in Docker run

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

 

Passing Spring Profile in DockerCompose

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.

]]>
http://www.littlebigextra.com/use-spring-profiles-docker-containers/feed/ 3