Spring Boot profiles Example – JavaDream
November 14, 2019 | Spring boot complete tutorial with example | No Comments
In this post we will see spring boot profiles. We know that our application go through many phases like development, testing, production. So for them we have to do configuration accordingly.
Spring boot provides profile feature for this. So Using spring boot profiles we can use different properties in different phases. Suppose i have three environment development, testing, production.
Now if we want that developer will use property defined for developers and tester will use properties defined for tester and in production our application use properties for production environment.
To see How to set profiles in spring boot we have to follow the below steps:
Project Structure:

1- Add below dependency in your pom.xml file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
2- create application.properties for development and testing phase syntax would be like
application-{Your-Phase-Name}.properties Eg: application-dev.properties // this is for development phase application-test.properties //this is for testing phase
application-dev.properties – In this property file we will define the property that will be used by the developer. Suppose i define only one property with key as name and value as development.
name=development environment
application-test.properties – In this property file we will define the property that will be used by the tester. And here we define only one property same as in developer property with key as name and value as testing.
name=testing environment
3- application.properties – This is our main property file here we define which profile need to be activated. Suppose currently developer is working on the application so our application should fetch property from application-dev.properties file so for this we have to write below property in our application.properties file.
spring.profiles.active=dev
Above syntax define that our application-dev.properties is activated. Now suppose the application is in testing phase so now tester wants that his property will be fetched so for using testing profile we will use below property in our application.properties file
spring.profiles.active=test
Now Check this using code suppose i want to print the name property value that i have defined in both the dev and test property. Suppose first i want to print the name from dev property than in my main application.properties file i will use spring.profiles.active=dev
And when we have to print the name from test property than in my main application.properties file i will use spring.profiles.active=test
Now create a controller class and get the value from dev or test profiles.
4- Create a controller with a rest end point that return name property value for different phase.
@RestController public class ProfileController { @Value("${name}") private String name; @GetMapping("/profile") public String testProfile() { return "Current Envirnment is "+ name; } }
5- Now Run your application and hit the URL
OUTPUT:
Set active profile to dev in your application.properties file


Set active profile to test in your application.properties file


Find Complete Code on GitHub- [ Spring Boot profile GithubUrl ]
You may also like.
Spring boot Actuator for production environment monitoring Example
How to use multiple databases with single spring boot project.