How do you read properties from application.properties in Spring Boot?

Table of Contents

Introduction

In Spring Boot, externalizing configuration is an important practice that makes applications flexible, portable, and easy to manage across different environments. One common method of externalizing configuration in Spring Boot is using the application.properties file (or application.yml). This file contains key-value pairs that Spring Boot can automatically load at startup.

Spring Boot provides several ways to read and inject properties from application.properties into your Spring beans, making it easier to manage application-specific configuration. The most common methods include:

  • Using the @Value annotation
  • Using @ConfigurationProperties for binding properties to a Java object
  • Accessing properties via the Environment object

This guide explains how to use these methods to read properties from application.properties in your Spring Boot applications.

Methods to Read Properties from application.properties in Spring Boot

1. Using the **@Value** Annotation

The @Value annotation is one of the simplest ways to inject a property value directly into fields, methods, or constructor parameters. It allows you to inject individual property values from application.properties or application.yml into Spring beans.

Example:

Consider the following application.properties file:

To inject these values into a Spring bean, use the @Value annotation:

In this example:

  • The @Value("${app.name}") injects the value of app.name from application.properties into the appName field.
  • Similarly, @Value("${app.version}") injects the app.version value into the appVersion field.

The values will be automatically resolved by Spring Boot during application startup and injected into the bean.

2. Using **@ConfigurationProperties**

@ConfigurationProperties is a more structured way of mapping properties to a Java class. It allows you to group related properties together in a strongly-typed Java bean. This approach is particularly useful for managing large sets of properties or configurations related to specific features or modules.

Example:

Consider the following application.properties file:

You can bind these properties to a Java class using @ConfigurationProperties:

In this example:

  • The @ConfigurationProperties(prefix = "app") annotation tells Spring Boot to bind all properties that start with app (e.g., app.name, app.version, and app.description) to the AppConfig class.
  • Spring Boot automatically converts the properties from application.properties into Java bean properties.

To enable @ConfigurationProperties, ensure that @EnableConfigurationProperties is added to your @SpringBootApplication class or another configuration class:

This method provides a more structured approach when dealing with related configuration values, and it also supports more complex configuration mappings (e.g., lists, nested objects, etc.).

3. Using the **Environment** Object

The Environment interface in Spring allows you to programmatically access property values. This method is useful when you need to retrieve properties dynamically and handle fallback values or more complex property logic.

Example:

Consider the following application.properties file:

You can use the Environment object to read these properties in a Spring bean:

In this example:

  • The Environment object is injected into the ServerConfig class.
  • The getProperty method is used to retrieve the server.host and server.port values from the application.properties file.
  • This approach allows more programmatic control, as you can also handle default values and check for property existence.

Accessing Default Values:

You can specify default values when a property is not found:

In this case, if server.host or server.port is not defined in application.properties, the default values "defaultHost" and "8080" will be used.

Best Practices for Reading Properties in Spring Boot

  1. Use **@Value** for Simple Property Injection: For simple cases where you just need to inject a single property, @Value is the most convenient and readable method.
  2. Use **@ConfigurationProperties** for Grouped Properties: When dealing with a collection of related properties, such as those that belong to the same module or feature, use @ConfigurationProperties to organize your configuration and avoid cluttering the code with many @Value annotations.
  3. Leverage Profiles for Environment-Specific Configuration: Spring Boot allows you to define different configurations for different environments using profiles. For example, you can have application-dev.properties for development and application-prod.properties for production, and Spring Boot will automatically load the appropriate file based on the active profile.
  4. Use the **Environment** for Programmatic Access: If you need to access properties dynamically or use them in non-bean components, you can use the Environment object.

Conclusion

In Spring Boot, reading properties from application.properties is easy and flexible. You can choose the method that best suits your needs:

  • Use the @Value annotation for quick, individual property injection.
  • Use @ConfigurationProperties to group related properties into a structured Java bean.
  • Use the Environment interface when you need programmatic access to property values.

By externalizing configuration in this way, you can easily manage different settings for various environments (e.g., development, production) without changing the application's code. This makes Spring Boot applications highly configurable and adaptable to different deployment scenarios.

Similar Questions