Spring boot enables to access a value defined in the application.properties file in different ways, such as using the @Value annotation, using System environment object, Using the @ConfigurationProperties Annotation, and so on. The property values defined in the application properties can be accessed in many ways, depending on how you want to use them in your spring boot application.
Spring boot application properties may vary depending on the environment, such as local development, QA, integration testing, and production. Separate environment property files may be configured and modified independently without impacting other environments in spring boot. A property value defined in the application.properties file can be accessed in different ways in Spring Boot
The application-related external configuration can be saved in a text file as a key-value pair. At the start of the application, the text property file is read and the value is assigned to the java variables. The Spring Boot framework contains techniques for accessing a value defined in the application.properties file.
application.properties file
Spring Boot creates a default property file called application.properties, which is stored in the project’s src/main/resources/ directory. When you first start a spring boot project, the default application.properties file is empty. The application.properties file is created with the environment name for each environment with post fix.
application-qa.properties
server.port=8080
application-prod.properties
server.port=80
application.yml file
Another method to define properties in a spring boot application is to use the yaml format. The spring boot application’s default yaml file is application.yml, which is located in the src/main/resources directory. The only difference would be in the presentation of the file’s contents, therefore the decision is entirely subjective.
application-qa.yml
server
port:8080
application-prod.yml
server
port:80
Using the @Value Annotation
The @Value annotation is the most frequent way to retrieve the property value in an application property file. The @Value annotation in spring boot reads the value from the application properties file and assigns it to a java variable. To read the property, the property key name must be provided in the @Value annotation.
If the property stated in the @Value annotation does not exist in any property file, the data type’s default initialization will be used. In the case, the default value can be assigned in the @Value annotation.
When the application starts or reloads the application properties file, the @Value annotation will load the value from the properties file. If you change the value in the properties file, the change will not be reflected until the application is restarted.
@Value("${spring.application.name}")
private String applicationName;
@Value("${spring.application.title:Yawin Tutor}")
private String applicationTitle;
Using System Environment Object
The another way of accessing property value from application.properties file is to autowire the environment object and use the getProperty() method in the environment object. The getProperty() method takes a single mandatory parameter, a string containing the property name, and returns the value of the property if it exists. The method returns null if the property does not exist.
When you call the getProperty method on the Environment object, it will read a value from the environment. The latest value from the application properties file will be returned by the environment object.
@Autowired
private Environment env;
public String getTitle() {
String applicationName = env.getProperty("spring.application.name");
}
Using the @ConfigurationProperties Annotation
Unlike the previous solutions, which accessed the properties separately, the @ConfigurationProperties annotation is used to access related collections of properties by saving the configuration in a POJO and using the POJO for future reference.
The @ConfigurationProperties annotation will map values based on the collection of properties’ prefix.
application.properties
spring.application.name=Yawin
spring.application.title=Yawin Tutor
spring.application.description=Yawin Tutor website
com.yawintutor.ApplicationConfiguration.java
package com.yawintutor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "spring.application")
@Configuration
public class ApplicationConfiguration {
private String name;
private String title;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
TestController.java
package com.yawintutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/scheduler")
public class TestController {
@Autowired
ApplicationConfiguration applicationConfiguration;
@GetMapping(value = "/name")
public String getName() {
String name = applicationConfiguration.getName();
return name;
}
}