Explain Codes LogoExplain Codes Logo

How to access a value defined in the application.properties file in Spring Boot

java
spring-boot
property-access
configuration-properties
Alex KataevbyAlex Kataev·Jan 1, 2025
TLDR

To retrieve a value from application.properties in Spring Boot, the @Value annotation will be your best companion. Here's how to use it:

@Value("${propertyName}") private String propertyValue;

Replace propertyName with your specific property key. This will allow you to inject the desired property value directly into your Spring component.

In Spring Boot, the application.properties file serves as an organized storage system of key-value pairs used for configuring diverse parts of a Spring application. Beyond just simple value access, @Value enables properties to be configured dynamically based on different environments.

Breaking Down Property Access

Direct Property Access Using @Value

The core way to access properties is to use the @Value annotation. It's straightforward, easy, and just works:

// It's like getting that first cup of coffee in the morning - smooth and aromatic! @Value("${property.name}") private String propertyName;

Using Environment for Property Retrieval

When @Value doesn't quite fit—perhaps you need dynamic keys or are operating outside the lifecycle of a bean—Environment becomes your lifeline:

@Autowired private Environment env; public String getDynamicProperty(String key) { return env.getProperty(key); }

The Environment class serves as your swiss army knife for accessing the property landscape of your application.

Binding Properties to Structured Objects

For reducing potential errors and when dealing with a group of related properties, @ConfigurationProperties enables you to relate .properties or .yml files to corresponding POJO fields making it less prone to error:

@ConfigurationProperties(prefix="service.details") public class ServiceDetails { private String url; private int timeout; // don't forget those getters and setters }

Don't forget to create getters and setters for each field to ensure they are properly bound.

Custom Property Sources

Need to externalize configuration or pick a different .properties file? @PropertySource along with @ConfigurationProperties is the way:

@Configuration @PropertySource("classpath:custom.properties") @ConfigurationProperties(prefix="custom") public class CustomConfig { // ... }

Here you're saying "Hey Spring, treat this as a bean factory and do your magic!"

Lifecycle-aware Property Injection

Be aware of @Value in certain phases such as @PostConstruct because it might not work properly. Do resort to constructor injection or rely on Environment for a safer access.

YML and Properties Harmony

@ConfigurationProperties is your easy-going friend—it appreciates both .properties and .yml, allowing you to pick your preferred format.

Direct vs Managed Property Access

When you need direct and immediate property access, Autowired Environment provides a straight road. For managed and type-safe access, inject your ConfigProperties.

Advanced Tips and Tricks

Injecting Values Conditionally

For conditional properties that may be absent, use the : operator with @Value for a default value fallback:

@Value("${optional.property:default}") private String optionalProperty;

Safe Practice with @Value

Be very precise while specifying the property name in @Value; a typo can result in a late runtime error—Spring cannot resolve the placeholder if no match is found.

Clean Code with @Configuration

Annotating your POJO with @Configuration signifies cleaner, class-level standardization. This informs Spring that the class acts as a source of bean definitions.