Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In plain Spring, register a properties file with @PropertySource, then read its values through Environment or inject fixed values with @Value. In Spring Boot, application.properties and application.yml are usually loaded automatically. When the same key appears in multiple places, Spring resolves it according to property-source precedence.

How Spring properties work

Spring’s Environment represents the application’s profiles and properties. Properties are stored in ordered PropertySource objects—abstractions over key-value sources such as files, JVM system properties, and operating-system environment variables. When sources contain the same key, the source with higher precedence supplies the resolved value.

This ordering matters as much as the property name: a value in a deployment environment may override one from a file, depending on how the sources are registered and which Spring setup is in use.

Load a properties file in plain Spring

In a Spring Framework application that does not rely on Boot’s automatic configuration-file loading, add @PropertySource to a configuration class:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Configuration
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig { }

Place foo.properties on the classpath, such as in the application’s resources. Spring 3.1 introduced @PropertySource as a declarative way to add a source to the Environment. You can repeat the annotation to register multiple files. A placeholder can also select a file, for example classpath:persistence-${envTarget:mysql}.properties; the default in this example selects mysql when envTarget is not supplied.

For duplicate keys among property sources, precedence determines the winning value. The Spring Framework reference describes the ordering and explains how to add a custom source with MutablePropertySources: Spring Environment and PropertySource.

Read values with @Value or Environment

Inject a fixed value with @Value

Use @Value when a Spring-managed bean needs a small number of known values:

@Value("${jdbc.url}")
private String jdbcUrl;

A colon supplies a fallback if the placeholder has no configured value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Value("${jdbc.url:aDefaultUrl}")
private String jdbcUrl;

The fallback is part of the placeholder expression; use a value appropriate to the application rather than relying on a default that could hide a missing deployment setting.

Look up values through Environment

Use Environment when the key is chosen at runtime, when code must test whether a property exists, or when it needs programmatic access to the environment:

String jdbcUrl = environment.getProperty("jdbc.url");

The Framework API also supports checking for a key before reading it. This avoids treating a missing property as though it were a configured value. Reference: Spring Framework Environment documentation.

Approach Best suited to How lookup happens
@Value A small number of fixed values used directly by a bean Spring resolves the placeholder during bean configuration; a colon can provide a fallback.
Environment Dynamic keys, presence checks, or broader programmatic inspection Application code requests a value from the ordered property sources.

How Spring Boot loads application properties

Spring Boot conventionally detects application.properties or application.yml, so a typical Boot application does not need to register those files using @PropertySource. Boot also supports profile-specific files, such as application-staging.properties, imported configuration through spring.config.import, and external inputs including command-line arguments, JVM -D properties, and operating-system environment variables.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For the exact precedence rules and supported configuration locations, consult the documentation for the Boot version used by the application: Spring Boot externalized configuration. Precedence is version- and source-specific; do not assume that a particular value wins without checking the applicable reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a source and manage overrides deliberately

Use the source that fits how the application is run: a classpath file for packaged defaults, profile-specific or imported configuration for environment separation, and deployment-provided values such as environment variables or command-line options for operational overrides. Keep the intended override order clear so that a value is not accidentally shadowed by another source.

  • Use @PropertySource to register additional files explicitly in plain Spring.
  • Use Boot’s conventional application files for standard Boot configuration rather than registering them again without a reason.
  • Use @Value for a few fixed bean values and Environment for lookups that depend on runtime conditions.
  • Keep secrets out of committed configuration files; supply sensitive values through an appropriately managed deployment mechanism.

For larger sets of related, type-converted settings in Spring Boot, review the current configuration-properties documentation before choosing a binding approach: Spring Boot configuration properties and external configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.