Introduction
Spring Boot simplifies the configuration of applications by providing a straightforward way to externalize properties.
These properties are often defined in the application.properties
file, and accessing them is a common task in Spring Boot development.
In this article, we will explore 3 most used approaches for accessing values defined in the application.properties
file.
We will be using the @Value
annotation, the @ConfigurationProperties
annotation and using Environment class for that.
Let’s say we have a set of properties in a application.propeties file:
custom.propertyname=string property value
custom.integer-value=100
In the following section we will see how to access this properties.
Using @Value Annotation
When to use @Value to access a property?
Accessing a property using @Value is the most commonly used approach. Unless there is a specific or non-standard requirement, this approach is suitable for the majority of situations.
How to use @Value to access a property?
The @Value
annotation in Spring allows developers to inject values directly into fields or methods. Here’s a how it works.
@Component
public class YourClass {
@Value("${custom.propertyname}")
private String yourPropertyValue;
// Rest of your code
}
In this example, "custom.propertyname"
corresponds to the key defined in the application.properties
file. The value of this property will be injected into the yourPropertyValue
field.
We can then use the injected value in methods within the class:
public void someMethod() {
System.out.println("Property Value: " + yourPropertyValue);
}
Using @ConfigurationProperties Annotation
When to use @ConfigurationProperties to access a property?
This approach is particularly useful when dealing with a larger set of configuration properties, providing better organization and maintainability.
It is also suitable if we deal with hierachical properties.
How to use @ConfigurationProperties to access a property?
The @ConfigurationProperties
annotation provides a more structured approach for binding properties to a Java class.
Here’s how to use it:
@Component
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
private String propertyname;
private Int integerValue;
// getter and setter for each property
}
In this example, properties with the prefix “custom” will be bound to this class. The @ConfigurationProperties
annotation eliminates the need to use @Value
for each property individually.
Now, you can use the configuration class in your component:
@Component
public class YourClass {
@Autowired
private CustomProperties customProperties;
public void someMethod() {
System.out.println("Property Value: " + customProperties.getPropertyname());
}
}
Using Environment Class
When to use Environment class?
This approach is especially useful when we need more dynamic access to environment-related information within your components or services.
It provides a programmatic way to retrieve configuration properties and other environment details at runtime.
How to access property using Environment?
We can auto-wire the org.springframework.core.env.Environment
directly into our components or beans to access environment-related information.
The Environment
interface provides methods to retrieve properties, profiles, and other configuration details. Here’s how we can auto-wire it and use it to access to a property
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class YourClass {
private final Environment environment;
@Autowired
public YourClass(Environment environment) {
this.environment = environment;
}
public void someMethod() {
// Accessing properties using Environment
String yourPropertyValue = environment.getProperty("custom.propertyname");
System.out.println("Property Value: " + yourPropertyValue);
// Accessing active profiles
String[] activeProfiles = environment.getActiveProfiles();
System.out.println("Active Profiles: " + Arrays.toString(activeProfiles));
}
}
Conclusion
In a Spring Boot application, accessing values from the application.properties
file is a common task. Whether using the @Value
annotation for direct injection or the @ConfigurationProperties
annotation for a more structured approach, developers have flexibility based on their preferences and project requirements.
For more details you can consult the spring boot official documentation.