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.
Key Points: • The Environment interface provides access to application configuration properties. • Properties can be loaded from multiple sources such as properties files, environment variables, and command-line arguments. • The getProperty() method is commonly used to retrieve property values. • Spring Boot also supports @Value and @ConfigurationProperties for property access. • Environment properties help create flexible and configurable applications.
Why Do We Need Environment Properties?
Applications often require configurable values such as:
• Database URLs • Usernames and passwords • API keys • Server ports • Feature flags
Instead of hardcoding these values, they are stored externally and loaded through Spring Boot's configuration mechanism.
Example:
application.properties
server.port=8081
app.name=Employee Service
database.url=jdbc:mysql://localhost:3306/companyAccessing Properties Using Environment Interface
Code Example:
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);
}
}Output:
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
Code Example:
@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.
Code Example:
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
Code Example:
@Value("${app.name}")
private String appName;Suitable for reading individual properties.
2. Using @ConfigurationProperties
Code Example:
@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.
Example: Suppose an application has the following configuration:
application.properties
app.name=Employee Management System
server.port=8081Using Environment:
String appName =
env.getProperty("app.name");Result:
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
• application.properties • application.yml • Environment variables • JVM system properties • Command-line arguments • External configuration files
Priority Order
Command Line Arguments ↓ Environment Variables ↓ application.properties ↓ Default Values
Spring Boot automatically resolves the highest-priority value.
Benefits
• Externalized configuration • Environment-specific settings • Improved maintainability • No hardcoded values • Easier deployment across environments
Real-World Example
A banking application may have different database configurations:
Development:
database.url=jdbc:mysql://dev-server/bankdb
Production:
database.url=jdbc:mysql://prod-server/bankdb
Using the Environment interface, the application can retrieve the correct value without any code changes.
Interview Tip: 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."