Spring Boot Profiles Example

The Spring Boot Profiles example show you how to use @Profile in Spring Boot application. @Profile annotation is introduced as part of the Spring Framework 3.1 release. @Profile annotation helps developers to create multiple configurations details for different environments. With Spring 4, the @Profile annotation can be used in the method level. That gives us more control and eliminate the need for creating multiple configuration files.

Let’s begin:

Project Directory Structure
In this Spring Boot Profile example, we are creating the project with the following structure

Spring Boot Profiles example

Project Dependency
In this Spring Boot Profiles example, we are using two dependencies: spring-boot-starter and spring-boot-starter-test

Create a service using @Profile annotation

In Spring Boot, the default profile is ‘default’. Let see the following speaking service.

Dig deeper: The syntax for the Profile annotation

Here, we also create another service using another profile

A Properties file

Create Spring Boot Application
Don’t forget to create the Spring Boot Application and sometimes it is call to be Spring Boot Starter

Demo our Spring Boot Profiles example
Go to the project directory then package our application by running the command line

Then run the jar file and see the result on the console

We change the current profile using parameters spring.profiles.active, like below

That’s all on the Spring Boot Profiles example.

References
Spring Boot – Profiles

Download complete source code, click link below

spring-boot-profile-example.zip (16 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons