How to access a value defined in the application.properties file in Spring Boot
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Spring Boot lets you read configuration values from application.properties in several different ways. The right choice depends on whether you need one simple value, ad hoc programmatic access, or a structured group of related settings.
Use @Value for Small, Simple Cases
For one or two straightforward properties, @Value is often enough.
Given this property:
you can inject it like this:
This is convenient, but it becomes harder to maintain if you use it for many related settings spread across many classes.
Use Environment for Programmatic Lookup
If you want to query properties dynamically, inject Spring's Environment.
This is useful when the property name is not hard-coded at the field level or when you want default-value behavior in code.
Prefer @ConfigurationProperties for Groups of Settings
For anything beyond a couple of isolated values, @ConfigurationProperties is usually the cleanest solution because it gives you a typed configuration object.
Properties:
Java configuration class:
And enable scanning or registration:
This approach scales much better because related settings stay grouped and type-safe.
Remember That Properties Can Come from Multiple Sources
Even though the question says application.properties, Spring Boot's configuration system can resolve values from:
- '
application.properties' - '
application.yml' - environment variables
- command-line arguments
- external config files
That means the value you read may have been overridden at runtime. The access API stays the same, but the actual source may differ.
That is one reason @ConfigurationProperties is often preferred in larger applications: it gives you one place to document, validate, and reason about the final configuration shape instead of scattering raw property lookups throughout the codebase.
Common Pitfalls
The biggest mistake is using @Value for large configuration groups. It works at first, but it becomes noisy, stringly typed, and difficult to validate as the settings grow.
Another issue is misspelling the property name and getting a failure or unexpected null. Property keys are easy to mistype, especially with long prefixes.
Developers also sometimes assume the value must come only from application.properties. In reality, Spring Boot merges multiple configuration sources and applies a precedence order, so the final value may be coming from an environment variable or command-line override.
Finally, avoid reading configuration from unmanaged objects. Injection works when the class is a Spring-managed bean; it will not work automatically in plain objects created with new.
Summary
- Use
@Valuefor a small number of simple properties. - Use
Environmentwhen you want programmatic access to configuration keys. - Use
@ConfigurationPropertiesfor grouped, typed, maintainable configuration. - Spring Boot may resolve the value from several configuration sources, not only
application.properties. - Prefer Spring-managed beans when you want property injection to work correctly.
Related reading
- How to access a value defined in the application.properties file in Spring Boot
- How to access entity manager with spring boot and spring data
- How to access outer class from an inner class?
- How to access Spring-boot JMX remotely
- How to acknowledge current offset in spring kafka for manual commit
- How to activate JMX on my JVM for access with jconsole?
- How to add a custom health check in spring boot health?
- How to add a dependency to a Spring Boot Jar in another project?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.