How to read data from java properties file using Spring Boot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Spring Boot is designed to read external configuration with very little setup. In most applications, the simplest answer is to place values in application.properties or application.yml and bind them with @Value, @ConfigurationProperties, or the Environment API depending on how structured the configuration is.
Read a Single Property with @Value
For one or two simple settings, @Value is fine:
This is easy to understand, but it becomes noisy once you have many related properties.
Prefer @ConfigurationProperties for Groups of Settings
When several settings belong together, a typed configuration class is cleaner:
Then enable it:
Now other beans can depend on ClientProperties as one coherent object.
Use Environment for Dynamic Lookups
Sometimes you need to read properties dynamically rather than bind them up front:
This is useful when the property name is conditional or when inline defaults are convenient.
Spring Boot Already Reads application.properties
One important point is that you usually do not need to load application.properties manually. Spring Boot automatically reads standard configuration files from the classpath and external config locations.
That means adding @PropertySource for the default application file is usually unnecessary.
Use a Custom Properties File Only When Needed
If you truly need an extra custom file, @PropertySource can load it:
After that, the properties are available through @Value or Environment just like other property sources.
In many projects, however, a separate custom file is not needed. Keeping most configuration in application.properties and profile-specific files is simpler.
Profile-Specific Configuration
Spring Boot also supports profile-specific files such as:
- '
application-dev.properties' - '
application-prod.properties'
This is often a better answer than making many custom property files because it matches Spring Boot's native configuration model.
Common Pitfalls
- Using
@Valueeverywhere even when a typed configuration class would be clearer. - Adding
@PropertySourceforapplication.propertieseven though Spring Boot already loads it. - Putting custom property files in the wrong location so they never reach the classpath.
- Scattering configuration lookups throughout the codebase instead of grouping related settings.
- Ignoring missing-property behavior until it fails at runtime in a less obvious place.
Summary
- Put standard configuration in
application.propertiesorapplication.yml. - Use
@Valuefor a small number of simple properties. - Use
@ConfigurationPropertieswhen several related settings belong together. - Use
Environmentfor dynamic lookups and inline defaults. - Reach for
@PropertySourceonly when you truly need an additional custom properties file.

