Category: java
What are Spring Profiles?
Published on 24 Jul 2026
Explanation
Spring Profiles allow you to maintain different configurations for different environments such as development, testing, and production. By activating a specific profile, Spring Boot loads only the configuration properties associated with that environment.
Code:
# application.properties spring.profiles.active=dev
Explanation
Spring Boot supports separate property files for each environment. For example, application-dev.properties and application-prod.properties can contain different database URLs, logging levels, and server configurations without changing the application code.
Code:
application.properties application-dev.properties application-test.properties application-prod.properties
Explanation
The @ConfigurationProperties annotation maps multiple configuration values from property files into a Java class. It provides type safety, better organization, and is preferred over multiple @Value annotations for related properties.
Code:
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String version;
// Getters and Setters
}
Explanation
Use the @Value annotation to inject individual property values directly into Spring-managed beans. It is suitable for reading a small number of configuration values from application.properties or environment variables.
Code:
@Value("${server.port}")
private int serverPort;
@Value("${spring.application.name}")
private String appName;
Explanation
Externalized Configuration allows application settings to come from environment variables, command-line arguments, Docker containers, or cloud platforms. This makes applications portable and eliminates the need to rebuild the application for configuration changes.
Code:
# Command Line java -jar app.jar --server.port=9090 # Environment Variable export SERVER_PORT=9090