Wednesday, 25 May 2022

Multi document yaml file demo in spring boot

From spring boot 2.4.0 onwards, you should use ‘spring.config.activate.on-profile’ to specify the properties to be active in a profile.

 

application.yml

spring:
  profiles:
    active: qa
    
globalProp1: global_p1
---

spring:
  config:
    activate:
      on-profile: dev
    
prop1: dev_p1
prop2: dev_p2

---

spring:
  config:
    activate:
      on-profile: qa
      
prop1: qa_p1
prop2: qa_p2

---

spring:
  config:
    activate:
      on-profile: prod
      
prop1: prod_p1
prop2: prod_p2

 

In the above file,

a.   ‘qa’ profile Is active by default.

b.   I defined 3 profiles in the file, and common set of properties are defined at root level (Ex: globalProp1 is common to all the profiles).

 

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘multi-yaml-doc-property-file-demo’.

 

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>multi-yaml-doc-property-file-demo</artifactId>
  <version>1</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
  </parent>

  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Step 3: Create application.yml file under src/main/resources folder.

 

application.yml

spring:
  profiles:
    active: qa
    
globalProp1: global_p1
---

spring:
  config:
    activate:
      on-profile: dev
    
prop1: dev_p1
prop2: dev_p2

---

spring:
  config:
    activate:
      on-profile: qa
      
prop1: qa_p1
prop2: qa_p2

---

spring:
  config:
    activate:
      on-profile: prod
      
prop1: prod_p1
prop2: prod_p2

Step 4: Create ‘com.sample.app.controller’ package and define ConfigController class.

 

ConfigController.java

package com.sample.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

	@Autowired
	private Environment environment;

	@RequestMapping("/app-configs")
	public ResponseEntity<Map<String, String>> appConfigs() {

		String prop1 = environment.getProperty("prop1");
		String prop2 = environment.getProperty("prop2");
		String globalProp1 = environment.getProperty("globalProp1");

		Map<String, String> appProperties = new HashMap<>();
		appProperties.put("prop1", prop1);
		appProperties.put("prop2", prop2);
		appProperties.put("globalProp1", globalProp1);

		return ResponseEntity.ok(appProperties);

	}
}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
  public static void main(String[] args) {

    SpringApplication.run(App.class, args);

  }
}

Total project structure looks like below.




Build the project

Navigate to the folder where pom.xml is located and execute the command ‘mvn clean install’. Proceed with below steps once build is successful.

 

Run the app using prod profile

Execute below command.

java -jar -Dspring.profiles.active=prod ./target/multi-yaml-doc-property-file-demo-1.jar

 

Open the url ‘http://localhost:8080/app-configs’ in browser, you will see the properties configured for prod profile.




You can download the complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment