Complete Guide to Loading @Value Properties from YAML Files in Spring Boot

Dec 02, 2025 · Programming · 12 views · 7.8

Keywords: Spring Boot | YAML Configuration | @Value Annotation | Property Loading | Configuration Files

Abstract: This article provides an in-depth exploration of the mechanisms for loading @Value properties from YAML configuration files in Spring Boot applications. Through analysis of a typical configuration loading failure case, it explains YAML file format requirements, Spring Boot property loading order, and correct usage of the @Value annotation. The article also discusses timing issues when accessing @Value properties in constructors and provides practical solutions.

Overview of Spring Boot Property Configuration Mechanism

Spring Boot provides a comprehensive property configuration system that supports loading configuration information from multiple sources. According to official documentation, SpringApplication loads application.properties or application.yml files in the following specific order:

  1. /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

YAML (.yml) files are natively supported in Spring Boot as an alternative to .properties files, requiring no additional configuration.

YAML File Format Requirements and Common Pitfalls

In the provided case, the developer encountered a typical configuration loading issue:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'files.upload.baseDir' in string value "${files.upload.baseDir}"

Although the problem description shows the YAML file contains the correct property structure:

files:
  upload:
    baseDir: /Users/Thomas/Code/IdeaProjects/project1/files

Actual investigation revealed that the root cause was incorrect indentation in the YAML file. YAML is extremely sensitive to indentation, and incorrect indentation can cause property hierarchy parsing to fail.

Correct YAML Configuration Example

Ensuring proper indentation format in YAML files is crucial. Here is a standard YAML configuration example:

# Correct indentation format
files:
  upload:
    baseDir: /path/to/files
    maxSize: 10MB

# Other configurations
server:
  port: 8080
  servlet:
    context-path: /api

In Spring Boot applications, these properties can be injected using the @Value annotation:

@Component
public class FileService {
    @Value("${files.upload.baseDir}")
    private String uploadBaseDir;
    
    @Value("${files.upload.maxSize}")
    private String maxUploadSize;
    
    // Business logic code
}

Property Loading Timing and Constructor Access Issues

Another common issue is attempting to access @Value injected properties in constructors. As mentioned in supplementary answers, Spring's dependency injection occurs after object instantiation, so @Value annotated properties are not yet injected when the constructor executes.

Consider this incorrect example:

@Component
public class ProblematicService {
    @Value("${some.property}")
    private String someProperty;
    
    public ProblematicService() {
        // Incorrect: someProperty is null at this point
        System.out.println(someProperty);
    }
}

The solution is to use the @PostConstruct annotation or implement the InitializingBean interface:

@Component
public class CorrectService {
    @Value("${some.property}")
    private String someProperty;
    
    @PostConstruct
    public void init() {
        // Correct: properties are injected at this point
        System.out.println(someProperty);
    }
}

Production Environment Configuration Override Strategies

When deploying to production environments, it's often necessary to override development configurations. Spring Boot provides multiple ways to achieve configuration override:

  1. Command-line arguments: As shown in the example using --spring.config.location=/path/to/application-production.yml
  2. Environment variables: Using SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION environment variables
  3. Configuration file priority: External configuration files have higher priority than internal ones

Typical production environment configuration file structure:

# application-production.yml
files:
  upload:
    baseDir: /var/www/uploads
    maxSize: 50MB

spring:
  datasource:
    url: jdbc:mysql://production-db:3306/appdb
    username: prod_user
    password: ${DB_PASSWORD}

Debugging and Verifying Configuration Loading

When encountering configuration loading issues, follow these debugging steps:

  1. Verify YAML file syntax and indentation
  2. Check configuration file loading locations and priorities
  3. Use Environment API to verify properties are correctly loaded
  4. Review configuration loading information in Spring Boot startup logs

Example debugging code:

@SpringBootApplication
public class Application implements CommandLineRunner {
    @Autowired
    private Environment env;
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    public void run(String... args) {
        // Verify property loading
        String property = env.getProperty("files.upload.baseDir");
        System.out.println("Loaded property: " + property);
    }
}

Best Practice Recommendations

  1. Always use consistent indentation (recommended 2 or 4 spaces)
  2. Create separate configuration files for different environments
  3. Use configuration classes (@ConfigurationProperties) instead of multiple @Value annotations
  4. Verify configuration loading in unit tests
  5. Use IDE YAML syntax checking tools

By following these best practices, most configuration loading issues can be avoided, ensuring Spring Boot applications correctly load configuration properties across different environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.