[Java] [Spring Boot] I want to add my own property file and get the value with env.getProperty().

less than 1 minute read

Composition of demo app

Demo application configuration.png src/main/resources/test.properties Properties file added by. Get the values from this file.

In property file.png The state of ↑ is in the property file. Add property file to environment with #@PropertySource

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

public class PropertyTestApplication {

public static void main(String[] args) {
SpringApplication.run(PropertyTestApplication.class, args);


It can be added to the environment by specifying the path of the property file in the argument of @PropertySource().

@PropertySource must be declared with @Configuration, but Since @Configuration is declared in @SpringBootApplication, it is not necessary to declare it specially.

Refer to @PropertySource here

Get with env.getProperty().

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

public class PropertyGetComponent {

Environment env;

public void printProperty() {
String value = env.getProperty("test.property.key");
System.out.println(" The retrieved value is [" + value + "].");


The execution result of printProperty() is ↓

The retrieved value is [test value ].

I was able to get the value from the properties file I added with env.getProperty() :blush:

Referenced articles