Tuesday, 27 August 2019

Spring boot: How to access the property value from application.properties file?


Approach 1: Using @Value annotation

Example
@Configuration
public class EnvironmentConfig {

	@Value("${app.name}")
	private String appName;

	@Value("${app.version}")
	private String appVersion;

	.....
	.....
}

Find the complete working application.

application.properties
app.name=demoServer
app.version=1.23


EnvironmentConfig.java
package com.sample.app.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EnvironmentConfig {

	@Value("${app.name}")
	private String appName;

	@Value("${app.version}")
	private String appVersion;

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getAppVersion() {
		return appVersion;
	}

	public void setAppVersion(String appVersion) {
		this.appVersion = appVersion;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("EnvironmentConfig [appName=").append(appName).append(", appVersion=").append(appVersion)
				.append("]");
		return builder.toString();
	}

}


HomeController.java
package com.sample.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.config.EnvironmentConfig;

@RestController
public class HomeController {
	
	@Autowired
	EnvironmentConfig envConfig;

	@RequestMapping("/")
	public String homePage() {
		return "Welcome to Spring boot Application Development";
	}
	
	@RequestMapping("/config")
	public String configValues() {
		return envConfig.toString();
	}

}


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);

	}

}

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>springbootMisc</groupId>
	<artifactId>springbootMisc</artifactId>
	<version>1</version>

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

	<dependencies>

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

	</dependencies>
</project>


Total project structure looks like below.

Run App.java.

Open the url ‘http://localhost:8080/config’ in browser, you can see all the application properties.
You can download the complete working application from this link.

Approach 2: Using @ConfigurationProperties annotation

Approach 3: Using @PropertySource and Environment


ConfigProperties.java
package com.sample.app.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {

	@Autowired
	private Environment env;

	public String getConfigValue(String configKey) {
		return env.getProperty(configKey);
	}
}


HomeController.java
package com.sample.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sample.app.model.ConfigProperties;

@RestController
public class HomeController {

	@Autowired
	ConfigProperties configProp;

	@RequestMapping("/")
	public String homePage() {
		return "Welcome to Spring boot Application Development";
	}

	@RequestMapping("/config")
	public String configValues() {
		String appName = configProp.getConfigValue("app.name");
		String appVersion = configProp.getConfigValue("app.version");

		StringBuilder builder = new StringBuilder();
		builder.append("Application Name : ").append(appName).append(", Application Version : ").append(appVersion);

		return builder.toString();
	}

}

You can download the complete working application from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment