Read values from application.properties file in Spring Boot
This post will discuss how to read a value defined in the application.properties file in a Spring Boot application.
In Spring Boot, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments. This post will discuss how to read a value defined in the properties files.
1. @Value annotation
The simplest way is to use the @Value annotation to load variables from the application.properties.
Suppose in application.properties, we have a property project.name. Then the property values can be injected directly into your beans by using the @Value annotation, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ProjectDetails { @Value("${project.name}") private String projectName; public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } } |
Controller Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller { @Autowired public ProjectDetails bean; @RequestMapping("projectName") String getProjectName() { return bean.getProjectName(); } } |
2. Spring’s Environment abstraction
The property values can also be accessed through Spring’s Environment abstraction, as shown in the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class ProjectDetails implements EnvironmentAware { private String projectName; @Override public void setEnvironment(Environment environment) { this.projectName = environment.getProperty("project.name"); } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } } |
Another alternative is to simply inject Environment into our controller/bean.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class ProjectDetails { @Autowired private Environment environment; public String getProjectName() { return environment.getProperty("project.name"); } } |
That’s all about reading values from the application.properties file in Spring Boot.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)