0
0
SpringbootHow-ToBeginner · 4 min read

How to Externalize Configuration in Spring: Simple Guide

In Spring, you externalize configuration by placing settings in external application.properties or application.yml files outside your code. Spring Boot automatically loads these files from the classpath or external locations, letting you change configuration without modifying code.
📐

Syntax

Spring externalizes configuration mainly using application.properties or application.yml files. You define key-value pairs for settings like database URLs or ports. Spring reads these files automatically at startup.

You can also use environment variables or command-line arguments to override these settings.

properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
server.port=8080
💻

Example

This example shows a Spring Boot app reading external configuration from application.properties. The app prints the configured server port and database URL.

java
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Value("${server.port}")
    private String serverPort;

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

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

    @Override
    public void run(String... args) {
        System.out.println("Server Port: " + serverPort);
        System.out.println("Datasource URL: " + datasourceUrl);
    }
}
Output
Server Port: 8080 Datasource URL: jdbc:mysql://localhost:3306/mydb
⚠️

Common Pitfalls

  • Wrong file location: Spring looks for application.properties or application.yml in src/main/resources or external folders. Placing files elsewhere means Spring won't find them.
  • Incorrect property keys: Typos in keys like spring.datasource.url cause Spring to ignore them.
  • Not refreshing context: Changes to external config require restarting the app to take effect.
properties
Wrong:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.user=root

Right:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
📊

Quick Reference

  • Use application.properties or application.yml files for external config.
  • Place config files in src/main/resources or specify external path with --spring.config.location.
  • Use @Value or @ConfigurationProperties to inject config values.
  • Override config with environment variables or command-line args.

Key Takeaways

Put configuration in external files like application.properties or application.yml to keep code clean.
Spring Boot auto-loads config files from classpath or external locations for easy changes.
Use @Value or @ConfigurationProperties to access config values in your code.
Check file location and property names carefully to avoid config loading issues.
Restart your Spring app after changing external config to apply updates.