Three Ways to Read YML Config in SpringBoot: @Value, Environment & @ConfigurationProperties

This tutorial demonstrates three methods to read YML configuration in SpringBoot — using @Value annotations, the Environment abstraction, and @ConfigurationProperties binding — with complete code examples for each approach.

Java Captain
Java Captain
Java Captain
Three Ways to Read YML Config in SpringBoot: @Value, Environment & @ConfigurationProperties

Project Setup

Create a SpringBoot project via Spring Initializr with the Spring Web dependency. The example uses Spring Boot 2.6.10 and Java 8. The pom.xml includes spring-boot-starter-web, spring-boot-starter-test, and the optional spring-boot-configuration-processor to enable IDE hints for @ConfigurationProperties.

YML Configuration File

SpringBoot loads application.yml (or .yaml / .properties) from src/main/resources. Priority order: properties > yml > yaml. Key YML rules: case-sensitive keys, values require at least one space after the colon, indentation defines hierarchy (spaces only, no tabs), # starts a comment.

Example application.yml:

name: 候胖胖
age: 40
hobby: 瞎逛游,开玩笑,踢毽子
employee:
  name: 任肥肥
  age: 38
  hobby:
    - 睡觉
    - 吃饭
    - 摸鱼
remark1: '${name} 
 喜欢踢毽子'
remark2: "${employee.name} 
 喜欢上班摸鱼"

Note: single quotes disable escape processing; double quotes enable it. Property references like ${name} and ${employee.name} are resolved.

Method 1: @Value Annotation

Inject individual properties directly into fields using @Value("${property.path}"). Works for primitives, Strings, and comma-separated strings converted to arrays. Cannot bind a YML list (dash syntax) as a whole array — only individual elements via index (e.g., ${employee.hobby[0]}).

@Value("${name}")
private String name;
@Value("${age}")
private int age;
@Value("${hobby}")
private String[] hobby; // comma-separated string -> array
@Value("${employee.name}")
private String e_name;
@Value("${employee.age}")
private int e_age;
@Value("${employee.hobby[0]}")
private String e_hobby0;

Endpoint /getdata1 returns concatenated values.

Method 2: Environment Abstraction

Autowire org.springframework.core.env.Environment and call getProperty(String key). Retrieves any property by its full path. Also resolves property references and escape sequences exactly as defined in YML.

@Autowired
private Environment env;

String name2 = env.getProperty("name");
String e_name2 = env.getProperty("employee.name");
String remark1 = env.getProperty("remark1"); // single-quoted: literal 

String remark2 = env.getProperty("remark2"); // double-quoted: actual newline

Endpoint /getdata2 prints results to console.

Method 3: @ConfigurationProperties Binding

Create a POJO annotated with @Component and @ConfigurationProperties(prefix = "employee"). Fields matching the nested YML structure (name, age, hobby) are bound automatically, including dash-style lists into arrays/collections.

@Component
@ConfigurationProperties(prefix = "employee")
public class Employee {
    private String name;
    private int age;
    private String[] hobby;
    // getters, setters, toString
}

Inject the bean and access typed properties. Endpoint /getdata3 prints the full object.

IDE Support

Adding spring-boot-configuration-processor (optional) eliminates the red warning banner in IntelliJ IDEA and provides autocomplete for configuration keys in YML files.

Running the Demo

Start the application (default port 8080). Test endpoints: GET /getdata1 — @Value results GET /getdata2 — Environment results (check console) GET /getdata3 — @ConfigurationProperties results (check console)

Change port via server.port: 8888 in YML.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaConfigurationMavenSpringBoot@ConfigurationProperties@ValueEnvironmentYML
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.