Spring Framework FAQ: Top Questions
18. What is the Environment abstraction in Spring?
Spring’s Environment
abstraction provides access to environment variables, system properties, and application configuration (like application.properties
).
📘 Features:
- Access to profiles and property sources.
- Useful for conditional configuration and context-aware logic.
📥 Example:
@Autowired
private Environment env;
public void printEnv() {
System.out.println(env.getProperty("spring.datasource.url"));
}
🏆 Expected Output:
Prints the value of the datasource URL property from environment or config file.
🛠️ Use Cases:
- Reading config dynamically in code.
- Activating specific logic based on active profile.