Configuration Files
Spring Boot supports multiple configuration formats such as .properties and YAML, enabling developers to manage application settings effectively. The @ConfigurationProperties annotation allows binding of configuration values to Java objects using a specified prefix.
package tech.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "application")
public class ApplicationProperties {
private String title;
private String releaseVersion;
// Getters and setters omitted for brevity
}
This class maps proeprties like application.title and application.releaseVersion from the configuration file.
Externalized Configuration
Spring Boot enables external configuration through system properties, environment variables, or command-line arguments. Developers can override default values dynamically without modifying the source code.
package tech.example.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ApplicationMetadata {
@Value("${application.title}")
private String title;
@Value("${application.releaseVersion}")
private String version;
// Getters and setters omitted for brevity
}
This component uses the @Value annotation to inject values from the configuration into class fields.
Profile Management
Spring Boot supports environment-specific configurations through profiles. Developers can define different configuration classes or files for development, testing, and production environments.
package tech.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("development")
public class DevelopmentSettings {
// Configuration specific to development environment
}
@Configuration
@Profile("production")
public class ProductionSettings {
// Configuration specific to production environment
}
These configuration classes are conditionally loaded into the Spring context based on the active profile.
Secure Configuration with Encryption
To manage sensitive configuration data such as database credentials, Spring Boot allows integration with encrypted property sources. Developers can load encrypted values from both classpath and external files.
package tech.example.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
@Configuration
@PropertySources({
@PropertySource("classpath:secure.properties"),
@PropertySource(value = "file:${user.home}/application-secrets.properties", ignoreResourceNotFound = true)
})
public class SecureProperties {
@Value("${database.user}")
private String dbUser;
@Value("${database.secret}")
private String dbSecret;
// Getters and setters omitted for brevity
}
This configuration loads encrypted properties from both the classpath and a user-defined external file.
Dynamic Configuration Reload
Spring Boot, in combination with Spring Cloud, supports dynamic configuration reloading. Components annotated with @RefreshScope automatically update their configuration values when changes occur.
package tech.example.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@RefreshScope
public class DynamicProperties {
@Value("${application.message}")
private String message;
// Getters and setters omitted for brevity
}
This component will refresh its message value when triggered via the Actuator refresh endpoint.