Spring Boot
Application Properties
Value Access
Software Development
Java Programming

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.

Practice algorithms

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:

properties
app.title=Inventory Service

you can inject it like this:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class AppInfo {
6
7    @Value("${app.title}")
8    private String title;
9
10    public String getTitle() {
11        return title;
12    }
13}

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.

java
1import org.springframework.core.env.Environment;
2import org.springframework.stereotype.Component;
3
4@Component
5public class PropertyReader {
6
7    private final Environment environment;
8
9    public PropertyReader(Environment environment) {
10        this.environment = environment;
11    }
12
13    public String readTitle() {
14        return environment.getProperty("app.title");
15    }
16}

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:

properties
app.name=Inventory Service
app.timeout-seconds=30

Java configuration class:

java
1import org.springframework.boot.context.properties.ConfigurationProperties;
2
3@ConfigurationProperties(prefix = "app")
4public class AppProperties {
5    private String name;
6    private int timeoutSeconds;
7
8    public String getName() {
9        return name;
10    }
11
12    public void setName(String name) {
13        this.name = name;
14    }
15
16    public int getTimeoutSeconds() {
17        return timeoutSeconds;
18    }
19
20    public void setTimeoutSeconds(int timeoutSeconds) {
21        this.timeoutSeconds = timeoutSeconds;
22    }
23}

And enable scanning or registration:

java
1import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3
4@SpringBootApplication
5@ConfigurationPropertiesScan
6public class DemoApplication {
7}

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 @Value for a small number of simple properties.
  • Use Environment when you want programmatic access to configuration keys.
  • Use @ConfigurationProperties for 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.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
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.

Practice algorithms