24. Externalized Configuration
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.propertieswhen devtools is active). @TestPropertySourceannotations on your tests.@SpringBootTest#propertiesannotation attribute on your tests.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON(inline JSON embedded in an environment variable or system property) ServletConfiginit parameters.ServletContextinit parameters.- JNDI attributes from
java:comp/env. - Java System properties (
System.getProperties()). - OS environment variables.
- A
RandomValuePropertySourcethat only has properties inrandom.*. - Profile-specific application properties outside of your packaged jar (
application-{profile}.propertiesand YAML variants) - Profile-specific application properties packaged inside your jar (
application-{profile}.propertiesand YAML variants) - Application properties outside of your packaged jar (
application.propertiesand YAML variants). - Application properties packaged inside your jar (
application.propertiesand YAML variants). @PropertySourceannotations on your@Configurationclasses.- Default properties (specified using
SpringApplication.setDefaultProperties).
To provide a concrete example, suppose you develop a @Component that uses a name property:
import org.springframework.stereotype.*
import org.springframework.beans.factory.annotation.*
_@Component_
public class MyBean {
_@Value("${name}")_
private String name;
// ...
}
On your application classpath (e.g. inside your jar) you can have an application.properties that provides a sensible default property value for name. When running in a new environment, an application.properties can be provided outside of your jar that overrides the name; and for one-off testing, you can launch with a specific command line switch (e.g. java -jar app.jar --name="Spring").
![]() |
Tip |
|---|---|
The SPRING_APPLICATION_JSON properties can be supplied on the command line with an environment variable. For example in a UN*X shell: |
![[Tip]](Spring Boot Reference Guide_files/tip.png)