Yes, Spring Boot provides multiple ways to access environment and application properties at runtime. The most common approach is using the Environment interface, which allows developers to read values from application.properties, application.yml, environment variables, system properties, and command-line arguments.
Why Do We Need Environment Properties?
Instead of hardcoding these values, they are stored externally and loaded through Spring Boot's configuration mechanism.
application.properties
server.port=8081
app.name=Employee Service
database.url=jdbc:mysql://localhost:3306/companyAccessing Properties Using Environment Interface
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertyReader {
@Autowired
private Environment environment;
public void displayProperties() {
String appName =
environment.getProperty("app.name");
System.out.println(appName);
}
}Employee Service
How It Works
application.properties
|
Spring Environment
|
Environment Interface
|
getProperty()
|
Property Value Returned
Spring loads all available properties into the Environment object during application startup.
Accessing Multiple Properties
@Component
public class ConfigService {
@Autowired
private Environment env;
public void loadConfig() {
String url =
env.getProperty("database.url");
String port =
env.getProperty("server.port");
System.out.println(url);
System.out.println(port);
}
}Providing Default Values
If a property does not exist, a default value can be returned.
String appName =
env.getProperty(
"app.name",
"Default Application");This prevents null values when a property is missing.
Other Ways to Access Properties
1. Using @Value
@Value("${app.name}")
private String appName;Suitable for reading individual properties.
2. Using @ConfigurationProperties
@ConfigurationProperties(prefix = "database")
public class DatabaseConfig {
private String url;
private String username;
private String password;
}Suitable when multiple related properties need to be grouped together.
application.properties
app.name=Employee Management System
server.port=8081String appName =
env.getProperty("app.name");Employee Management System
This allows the same application to use different configurations across development, testing, and production environments without changing code.
Property Sources Supported by Spring Boot
Priority Order
Command Line Arguments
↓
Environment Variables
↓
application.properties
↓
Default Values
Spring Boot automatically resolves the highest-priority value.
Benefits
Real-World Example
database.url=jdbc:mysql://dev-server/bankdb
database.url=jdbc:mysql://prod-server/bankdb
Using the Environment interface, the application can retrieve the correct value without any code changes.
A concise interview answer is
Yes, we can access environment properties in Spring Boot using the Environment interface. By injecting the Environment object and calling the getProperty() method, we can retrieve values from application.properties, application.yml, environment variables, or other configuration sources. Spring Boot also provides alternatives such as @Value and @ConfigurationProperties for accessing configuration properties.
Last updated: 11 November 2026
Previous question: How do you list all the beans in a Spring Boot application?
Next question: How do you enable debug logging in a Spring Boot application?