0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Multiple Properties Files in Spring Boot

In Spring Boot, you can use multiple properties files by specifying them with @PropertySource annotations or by listing them in spring.config.import in application.properties. This allows your application to load configuration from several files and merge them automatically.
📐

Syntax

You can load multiple properties files in Spring Boot using the @PropertySource annotation on a configuration class or by using the spring.config.import property in your application.properties or application.yml.

@PropertySource syntax:

  • @PropertySource({"classpath:file1.properties", "classpath:file2.properties"}) loads multiple files.

spring.config.import syntax:

  • spring.config.import=classpath:file1.properties,classpath:file2.properties
java
@Configuration
@PropertySource({"classpath:config1.properties", "classpath:config2.properties"})
public class AppConfig {
    // Beans and configuration
}
💻

Example

This example shows how to load two properties files named config1.properties and config2.properties using @PropertySource. The properties from both files are available for injection using @Value or Environment.

java
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@PropertySource({"classpath:config1.properties", "classpath:config2.properties"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class MyController {

    @Value("${app.name}")
    private String appName;

    @Value("${app.version}")
    private String appVersion;

    @GetMapping("/info")
    public String info() {
        return "App: " + appName + ", Version: " + appVersion;
    }
}
Output
App: MySpringApp, Version: 1.0.1
⚠️

Common Pitfalls

1. Missing files: If a properties file is not found, Spring throws an error unless you set ignoreResourceNotFound=true in @PropertySource.

2. Property overriding: Properties in later files override earlier ones. Order matters.

3. Using @PropertySource only works with .properties files, not .yml.

4. For YAML files, use spring.config.import in application.properties or application.yml.

java
@PropertySource(value = "classpath:optional.properties", ignoreResourceNotFound = true)
// This prevents errors if the file is missing
📊

Quick Reference

FeatureUsageNotes
@PropertySourceLoad multiple .properties files in config classOnly supports .properties, not YAML
spring.config.importList multiple config files in application.propertiesSupports .properties and .yml files
ignoreResourceNotFoundAvoid errors if file missingUse in @PropertySource annotation
Property overridingLast file properties override earlier onesOrder matters in loading files

Key Takeaways

Use @PropertySource to load multiple .properties files in a configuration class.
Use spring.config.import in application.properties to load multiple config files including YAML.
Order of files matters; later files override earlier properties.
Set ignoreResourceNotFound=true in @PropertySource to avoid errors if files are missing.
For YAML files, prefer spring.config.import over @PropertySource.