Monday, 16 September 2019

Spring boot: YAML Configuration


You can configure properties to a spring application via a property file or via an YAML configuration file.

Example
Following is the simple yaml file that represents two profiles information.
# Test profile configurations
spring:
    profiles: test
name: test-YAML
environment: test
servers: 
    - www.abc.test.com
    - www.xyz.test.com
 
# Prod profile configurations
---
spring:
    profiles: prod
name: prod-YAML
environment: production
servers: 
    - www.abc.prodcom
    - www.xyz.prod.com
 
# : Specifies the comment, used to document additional information about configurations.

--- : Specifies the starting of new yaml document, so we can put all the profiles information in one yaml document only.

Where can I put application.yml file?
/{APPLICATION_PATH}/src/main/resources/application.yml.

Let’s build new application.

Step 1: Create new maven project ‘springApplicationYml’.

Step 2: Update pom.xml with maven dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>springApplicationYml</groupId>
 <artifactId>springApplicationYml</artifactId>
 <version>1</version>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
  </dependency>
 
 </dependencies>
</project>
 
Step 3: Create application.yml under src/main/resources folder.


application.yml
# Test profile configurations
spring:
    profiles: test
name: test-YAML
environment: test
servers: 
    - www.abc.test.com
    - www.xyz.test.com
 
# Prod profile configurations
---
spring:
    profiles: prod
name: prod-YAML
environment: production
servers: 
    - www.abc.prodcom
    - www.xyz.prod.com
 
Step 4: Map yaml configurations to a config class.

Create a package ‘com.sample.app.config’ and define YAMLConfig.java like below.


YAMLConfig.java
package com.sample.app.config;
 
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {
 
 private String name;
 private String environment;
 private List<String> servers = new ArrayList<>();
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getEnvironment() {
  return environment;
 }
 
 public void setEnvironment(String environment) {
  this.environment = environment;
 }
 
 public List<String> getServers() {
  return servers;
 }
 
 public void setServers(List<String> servers) {
  this.servers = servers;
 }
 
}
 
Step 5: Create App.java
App.java
package com.sample.app;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
 
import com.sample.app.config.YAMLConfig;
 
@SpringBootApplication
public class App {
 
 @Autowired
 private YAMLConfig yamlConfig;
 
 public static void main(String args[]) {
  SpringApplication.run(App.class, args);
 }
 
 @Bean
 public CommandLineRunner demo() {
  return (args) -> {
 
   System.out.println(yamlConfig.getName());
   System.out.println(yamlConfig.getEnvironment());
   yamlConfig.getServers().forEach(System.out::println);
 
  };
 
 }
}
 

Total project structure looks like below.

Run App.java by passing the vm argument ‘-Dspring.profiles.active=prod’, you can see below messages in console.

prod-YAML
production
www.abc.prodcom

How to pass VM argument in Eclipse?
Right click on App.java -> Run As -> Run Configurations….


Go to Arguments tab, under VM Arguments: tab, put the value -Dspring.profiles.active=prod to activate prod profile. Put the value -Dspring.profiles.active=test to activate test profile.


Previous                                                    Next                                                    Home

No comments:

Post a Comment