Tuesday, 16 November 2021

Spring: Reuse previously defined values in application.yml (or) application.properties file

Spring provides a way to refer back to previously defined values in application.yml or application.properties file. Just place the the standard ${name} property-placeholder syntax to access the value associated with the property name.

 

Example

app:
  name: Chat server
  version: 1.23
  description: ${app.name} is a spring boot applicaiton and app is at version ${app.version}

 

In the above snippet, app.description is set to the value "Chat server is a spring boot applicaiton and app is at version 1.23".

 

Find the below working application.

 

Step 1: Create new maven project ‘reuse-envt-variables’.

 

Step 2: Update pom.xml with the 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>reuse-envt-variables</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.6</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>



	<properties>
		<java.version>1.8</java.version>
	</properties>

	<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: Define AppController class.

 

AppController.java

package com.sample.app.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppController {

	@Value("${app.description}")
	private String appDescription;

	@GetMapping("/aboutMe")
	public String getDescription() {
		return appDescription;
	}

}

Step 4: 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);
	}

}

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

 

application.yml

app:
  name: Chat server
  version: 1.23
  description: ${app.name} is a spring boot applicaiton and app is at version ${app.version}

Total project structure looks like below.





Run App.java and open the url ‘http://localhost:8080/aboutMe’ in browser, you will see below message in the browser.

Chat server is a spring boot applicaiton and app is at version 1.23

You can download the complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/rest/reuse-envt-variables



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment