Spring Boot :Profile Specific Properties and Placeholders in properties

Kumar Pallav
Kumar Pallav
I am a passionate java programmer, Open Source and Microservices lover, blogger and online instructor. I create Simple Short Tutorials

Spring Boot :Profile Specific Properties and Placeholders in properties

• Kumar Pallav

Profile Specific Properties

Spring provides profiles for environment based properties. For example Database related properties will change for Development , QA and Production environment. For such cases we can create property file like

application-{profile}.properties

Like application-dev.properties , application-qa.properties or application-prod.properties .By default , if not specified profile is “default”, You can see this happening in logs as well. Below is the screenshot of same :

Consider a property like app.name defiled in application.properties and as well as application-{profile}.properties like application-dev.properties. If the profile is set to dev , the value for app.name in.properties will be considered. It will override the default one.

We will see profile in details in next chapter.

Placeholder in properties

Placeholders could be of great use when defining any properties file specially a long one with references from other properties. For example , see below properties

app.name=User Application
app.version= 1.0.0
app.description = ${app.name} - ${app.version} is an application to store user details.

If we add above properties in application.properties file , and try to fetch app.description property, It will refer app.name and app.version to create value for app.description. Let’s test it

We will add a Simple request mapping in our common controller and return these values via a map.

@Autowired

private Environment env;

@RequestMapping("app-info")

protected Map<String,String>appInfo(){

Map<String,String>appInfo= new LinkedHashMap<String,String>();

appInfo.put("name", env.getProperty("app.name"));

appInfo.put("version", env.getProperty("app.version"));

appInfo.put("description", env.getProperty("app.description"));

return appInfo;

}

Now if we hit the URL http://localhost:8081/v1/app-info , we will get following response validation that placeholders are working. (It can be noted that we have removed command line parameter of –server.port, that’s why it is running on 8081 picking from properties file )

{
    "name": "User Application",
    "version": "1.0.0",
    "description": "User Application - 1.0.0 is an application to store user details."
}

Here name and versions are replaced in description.