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
Project Dependency
In this Spring Boot Profiles example, we are using two dependencies: spring-boot-starter and spring-boot-starter-test
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> |
Create a service using @Profile annotation
In Spring Boot, the default profile is ‘default’. Let see the following speaking service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile({"speaking", "default"}) public class SpeakingService implements SoundService { @Override public String broadcast() { return "A man is speaking!"; } } |
Dig deeper: The syntax for the Profile annotation
1 2 |
@Profile("speaking"); // declare the speaking profile @Profile({"speaking", "default"});// declare two the speaking and default profiles |
Here, we also create another service using another profile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile("crying") public class CryingService implements SoundService { @Override public String broadcast() { return "A baby is crying!"; } } |
A Properties file
1 2 3 4 5 |
# In Spring, default profile is 'default' #spring.profiles.active=speaking // set default profile is speaking if need logging.level.=error spring.main.banner-mode=off |
Create Spring Boot Application
Don’t forget to create the Spring Boot Application and sometimes it is call to be Spring Boot Starter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javabycode; import com.javabycode.service.SoundService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySpringApplication implements CommandLineRunner { @Autowired private SoundService soundService; @Override public void run(String... args) throws Exception { System.out.println(soundService.broadcast()); } public static void main(String[] args) throws Exception { SpringApplication.run(MySpringApplication.class, args); } } |
Demo our Spring Boot Profiles example
Go to the project directory then package our application by running the command line
1 |
mvn package |
Then run the jar file and see the result on the console
1 2 |
java -jar target/spring-boot-profile-example-1.0.jar A man is speaking! |
We change the current profile using parameters spring.profiles.active, like below
1 2 |
java -jar -Dspring.profiles.active=raining target/spring-boot-profile-example-1.0.jar A baby is crying! |
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 (269 downloads)