The Spring Boot Profiles YAML Properties example shows you how to use Spring Profiles based on YAML and Properties. Spring Profiles help developers to create multiple configurations details for different environments. In Spring Boot, the default profile is default, we can set the profile via spring.profiles.active property.
In Spring Boot, we define profiles based on .properties or .yaml files following the syntax:
1 2 3 4 5 |
application-{profile}.properties OR application-{profile}.yml |
Let’s begin to create Spring Boot Profiles YAML Properties example:
Project Structure
In this Spring Boot Profiles Yaml Properties example, we are creating a project with the following structure:
Maven dependency
In this Spring Boot Profiles Yaml Properties 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> |
Define profiles based Properties
Multi profiles .properties example.
application.properties
1 2 |
#spring profile spring.profiles.active=dev |
application-dev.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#dev environment #Logging logging.level.org.springframework.web=ERROR logging.level.com.javabycode=DEBUG logging.level.=ERROR #spring spring.main.banner-mode=off #server server.email: dev@javabycode.com server.ip=127.0.0.1 server.path=/localhost |
application-prod.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#production environment #Logging logging.level.org.springframework.web=ERROR logging.level.com.javabycode=ERROR logging.level.=error #spring spring.main.banner-mode=off #server server.email: prod@javabycode.com server.ip=192.168.0.1 server.path=/javabycode.com |
Define profiles based YAML
Multi profiles .yml example. In YAML, we can create multiple profiles by using a “—” separator.
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
logging: level: .: error org.springframework: ERROR com.javabycode: ERROR spring: profiles: active: "dev" main: banner-mode: "off" --- spring: profiles: dev server: email: dev@javabycode.com ip: 127.0.0.1 path: /localhost --- spring: profiles: prod server: email: prod@javabycode.com ip: 192.168.0.1 path: /javabycode.com logging: level: .: error org.springframework: ERROR com.javabycode: ERROR |
Load resource properties using @ConfigurationProperties
Read the properties or yaml files later.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.javabycode.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties("server") public class ServerProperties { private String email; private String ip; private String path; public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setIp(String ip) { this.ip = ip; } public String getIp() { return ip; } public void setPath(String path) { this.path = path; } public String getPath() { return path; } @Override public String toString() { return "ServerProperties{" + "email='" + email + '\'' + "ip='" + ip + '\'' + "path='" + path + '\'' + '}'; } } |
Spring Boot application
Don’t forget to create the Spring Boot Application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javabycode; import com.javabycode.config.ServerProperties; 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 ServerProperties serverProperties; @Override public void run(String... args) throws Exception { System.out.println(serverProperties); } public static void main(String[] args) throws Exception { SpringApplication.run(MySpringApplication.class, args); } } |
Demo Spring Boot Profiles YAML Properties example
Go to the project directory, we package the jar file using the following command line.
1 |
mvn package |
Run the jar file and see output on the console
1 2 3 4 5 |
java -jar target/spring-boot-profiles-yaml-properties-1.0.jar 2017-04-09 09:40:13.496 DEBUG 13132 --- [ main] com.javabycode.MySpringApplication : Running with Spring Boot v1.5.2.RELEASE, Spring v4.3.7.RELEASE 2017-04-09 09:40:13.496 INFO 13132 --- [ main] com.javabycode.MySpringApplication : The following profiles are active: dev ServerProperties{email='dev@javabycode.com'ip='127.0.0.1'path='/localhost'} |
As you see the output above, our application is using the dev profile.
Now we try to change the profile via the spring.profiles.active parameter like below
1 2 3 |
java -jar -Dspring.profiles.active=prod target/spring-boot-profiles-yaml-properties-1.0.jar ServerProperties{email='prod@javabycode.com'ip='192.168.0.1'path='/javabycode.com'} |
Note: This demonstrates for the .yml file. Thus, the properties file is renamed to avoid conflicts. If you want to test .properties file, please rename it back to the correct syntax.
That’s all on the Spring Boot Profiles YAML Properties example.
References
Spring Boot – Profile-specific properties
Spring Boot Profiles Example
Download complete source code, click link below
spring-boot-profiles-yaml-properties.zip (345 downloads)