External Configuration in Spring Boot: Sources, Priorities, and Usage
This article explains how Spring Boot allows external configuration through various sources such as command‑line arguments, JNDI, system properties, environment variables, random values, property files, @PropertySource, and default properties, detailing their priority order and usage examples with code snippets.
Spring Boot enables external configuration so that the same application code can run in different environments by injecting properties or overriding defaults through configuration files.
Spring Boot supports multiple external configuration methods
The sources are applied in the following priority order:
Command line arguments
JNDI properties from java:comp/env
Java system properties ( System.getProperties() )
Operating‑system environment variables
RandomValuePropertySource configured random.* values
External application-{profile}.properties or application.yml (with spring.profile )
Internal application-{profile}.properties or application.yml (with spring.profile )
External application.properties or application.yml (without spring.profile )
Internal application.properties or application.yml (without spring.profile )
@Configuration class annotated with @PropertySource
Default properties set via SpringApplication.setDefaultProperties
Command line arguments
You can pass parameters when starting the jar, for example:
java -jar app.jar --name="Spring" --server.port=9090Parameters use the --key=value syntax and can be either custom or Spring Boot defaults. The --server.port example shows how to change the web port.
Note that command‑line arguments must appear after the jar name; they can be disabled with SpringApplication.setAddCommandLineProperties(false) .
Java system properties
System properties are supplied with the -D flag, e.g.:
java -Dname="isea533" -jar app.jarThey have the same effect as command‑line arguments but a different precedence.
Operating‑system environment variables
Environment variables such as JAVA_HOME can be used, but some OSes do not support dots in variable names; in that case use uppercase with underscores (e.g., SERVER_PORT ).
RandomValuePropertySource
Random values can be injected using placeholders like ${random.value} , ${random.int} , ${random.long} , etc. The random.int placeholder also supports max and value attributes to define ranges.
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}Application configuration files (.properties or .yml)
Properties can be defined directly in files:
name=Isea533
server.port=8080YAML format is also supported:
name: Isea533
server:
port: 8080Spring Boot searches for these files in /config on the classpath first, then in the classpath root.
@PropertySource
The @PropertySource annotation allows you to specify additional property files with lower priority.
SpringApplication.setDefaultProperties
You can programmatically set default properties:
SpringApplication application = new SpringApplication(Application.class);
Map
defaultMap = new HashMap<>();
defaultMap.put("name", "Isea-Blog");
application.setDefaultProperties(defaultMap);
application.run(args);Using properties in code
@Value("${xxx}")
The @Value annotation injects a single property value directly into a field.
@ConfigurationProperties
Properties with a common prefix can be bound to a POJO. Example:
my.name=Isea533
my.port=8080
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com @ConfigurationProperties(prefix="my")
public class Config {
private String name;
private Integer port;
private List
servers = new ArrayList<>();
// getters and setters
}Spring Boot automatically converts types and supports nested properties.
Using @ConfigurationProperties on @Bean methods
@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
...
}Properties prefixed with foo are bound to the returned bean.
Property placeholders
Placeholders can reference previously defined properties, e.g.:
app.name=MyApp
app.description=${app.name} is a Spring Boot applicationDefault values can be provided with the ${property:default} syntax.
Property name matching rules
For a field firstName in a class annotated with @ConfigurationProperties(prefix="person") , the following property names are valid:
person.firstName (camel case)
person.first-name (dash‑separated, suitable for .properties and .yml)
PERSON_FIRST_NAME (uppercase with underscores, suitable for environment variables)
Property validation
JSR‑303 annotations such as @NotNull can be used to validate configuration properties:
@Component
@ConfigurationProperties(prefix = "connection")
public class ConnectionSettings {
@NotNull
private InetAddress remoteAddress;
// getters and setters
}Conclusion
The above covers the main ways to configure and use properties in Spring Boot. For more details, refer to the official Spring Boot documentation and the Externalized Configuration guide.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.