Tuesday 17 September 2019

Spring boot: yml: How to specify common properties across profiles?


Any property that you specified out of yaml profile document (--- specifies end of the document) is common for all spring profiles.

For example,
projectTechName: chat server
---
 
# 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
---

noOfDevelopers: 12
 
In the above application projectTechName and noOfDevelopers are common to both prod and test profiles.


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("Name : " + yamlConfig.getName());
   System.out.println("Environment : " + yamlConfig.getEnvironment());
   System.out.println("Servers : ");
   yamlConfig.getServers().forEach(System.out::println);
 
   System.out.println("Project Technical Name : " + yamlConfig.getProjectTechName());
   System.out.println("No Of Developers: " + yamlConfig.getNoOfDevelopers());
 
  };
 
 }
}
 

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 String projectTechName;
 private List<String> servers = new ArrayList<>();
 private int noOfDevelopers;
 
 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;
 }
 
 public String getProjectTechName() {
  return projectTechName;
 }
 
 public void setProjectTechName(String projectTechName) {
  this.projectTechName = projectTechName;
 }
 
 public int getNoOfDevelopers() {
  return noOfDevelopers;
 }
 
 public void setNoOfDevelopers(int noOfDevelopers) {
  this.noOfDevelopers = noOfDevelopers;
 }
 
}
 
application.yml
projectTechName: chat server
---
 
# 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
---

noOfDevelopers: 12
 
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>
 
Total application looks like below.

Run App.java by passing the vm argument -Dspring.profiles.active=test, you will see below messages in console.
Name : test-YAML
Environment: test
Servers :
www.abc.test.com
www.xyz.test.com
Project Technical Name : chat server
No Of Developers: 12
Run App.java by passing the vm argument -Dspring.profiles.active=prod, you will get below messages in console.
Name :prod-YAML
Environment: production
Servers :
www.abc.prodcom
www.xyz.prod.com
Project Technical Name : chat server
No Of Developers: 12
You can download complete working application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment