Spring Boot – Profile based properties example.
A profile is a set of settings that can be configured to override settings from application.properties.
Each profile is contained in a file named application-profilename.properties where profilename is the name of the profile.
Now, a profile could configure anything you want, however for most projects it is proposed to have the following profiles:
dev for your local development settings
staging for your staging server settings
prod for your production settings
test for running your tests
Whether you have all above environment in your application or not, you must have a dev, a prod and a test
profile. The configuration for these environments needs to be different for obvious reasons. Now lets see how spring boot
manages profile based properties in any application. All you need is create different properties for different profiles or
environments as shown below:
application.properties |
cmdb.resource-url=http://java2novice.com
cmdb.resourcePort[0]=80
cmdb.resourcePort[1]=443
cmdb.resourceCount=2
|
application-dev.properties |
cmdb.resource-url=http://dev.java2novice.com
cmdb.resourcePort[0]=8080
cmdb.resourceCount=2
|
application-prod.properties |
cmdb.resource-url=http://java2novice.com
cmdb.resourcePort[0]=80
cmdb.resourcePort[1]=443
cmdb.resourceCount=2
|
Spring boot will bind all the properties to below class:
package com.java2novice.springboot.util;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties("cmdb")
public class CmdbProperties {
@NotEmpty
private String resourceUrl;
private List<Integer> resourcePort;
@Max(5)
@Min(0)
private Integer resourceCount;
@Override
public String toString() {
return "resourceUrl: "+ this.resourceUrl+"\n"
+ "resourcePort: "+this.resourcePort+"\n"
+ "resourceCount: "+this.resourceCount+"\n";
}
public String getResourceUrl() {
return resourceUrl;
}
public void setResourceUrl(String resourceUrl) {
this.resourceUrl = resourceUrl;
}
public List<Integer> getResourcePort() {
return resourcePort;
}
public void setResourcePort(List<Integer> resourcePort) {
this.resourcePort = resourcePort;
}
public Integer getResourceCount() {
return resourceCount;
}
public void setResourceCount(Integer resourceCount) {
this.resourceCount = resourceCount;
}
}
|
And here is the Spring boot application class:
package com.java2novice.springboot;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.java2novice.springboot.util.CmdbProperties;
@SpringBootApplication
public class SpringBootWebApplication {
private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);
@Autowired
private CmdbProperties cmdbProperties;
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@PostConstruct
public void init() {
logger.info(cmdbProperties.toString());
}
}
|
Now package the application and run: You can pass the profile values as part of command line arguments
by using "-Dspring.profiles.active". We will pass prod profile as an input:
Output: |
java -jar -Dspring.profiles.active=prod target/spring-boot-tutorials-0.0.1-SNAPSHOT.jar
resourceUrl: http://java2novice.com
resourcePort: [80, 443]
resourceCount: 2
|
Output: |
java -jar -Dspring.profiles.active=dev target/spring-boot-tutorials-0.0.1-SNAPSHOT.jar
resourceUrl: http://dev.java2novice.com
resourcePort: [8080]
resourceCount: 2
|
|