The application.properties file is a central mechanism for configuring your spring boot application. However, Spring Boot provides multiple ways to set and override these properties, offering flexibility for different environments, use cases, and deployment scenarios.
Below is a comprehensive list of ways to configure application properties in Spring Boot, ordered roughly by precedence.
1. Command-Line Arguments
- Properties can be passed as command-line arguments when running the application.
- Format: –property.name=value
- Example:
java -jar myapp.jar --spring.kafka.bootstrap-servers=localhost:9092
- Precedence: Highest (overrides all other sources).
2. Java System Properties
- Set properties via Java’s -D flag when launching the JVM.
- Example:
java -Dspring.kafka.bootstrap-servers=localhost:9092 -jar myapp.jar
- Precedence: Overrides most file-based configurations but is below command-line arguments.
3. Environment Variables
- Properties can be set as environment variables, following a specific naming convention (uppercase, dots replaced with underscores).
- Example:
export SPRING_KAFKA_BOOTSTRAP_SERVERS=localhost:9092 java -jar myapp.jar
- Nested properties use double underscores (__) for maps (e.g., SPRING_KAFKA_PROPERTIES__SECURITY_PROTOCOL).
- Precedence: Overrides file-based configs but is below command-line args and system properties.
4. Application Properties File (application.properties or application.yml)
- The default configuration file, typically located in src/main/resources.
- Example (application.properties):
spring.kafka.bootstrap-servers=localhost:9092
- Example (application.yml):
spring: kafka: bootstrap-servers: localhost:9092
- Precedence: Overridden by external files, environment variables, and command-line args.
- Profiles: Use application-{profile}.properties (e.g., application-dev.properties) for environment-specific settings.
5. Externalized Configuration Files
- Place application.properties or application.yml outside the JAR in the working directory or a custom location.
- Default locations (searched in this order):
- ./config/ (subdirectory of the current directory)
- ./ (current directory)
- /config/ (classpath)
- / (classpath root)
- Custom location via –spring.config.location:
java -jar myapp.jar --spring.config.location=file:/etc/myapp/config/
- Precedence: External files override classpath files.
6. Spring Cloud Config Server
- Fetch properties from a remote configuration server (e.g., backed by Git, Vault, or a database).
- Dependency:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- Bootstrap configuration (bootstrap.properties or bootstrap.yml):
spring: cloud: config: uri: http://config-server:8888 name: myapp profile: dev
- Precedence: Overrides local files unless disabled; refreshed dynamically with @RefreshScope.
7. RandomValuePropertySource
- Generate random values for properties using random.* syntax.
- Example:
my.secret=${random.value} my.number=${random.int(10)} my.uuid=${random.uuid}
- Precedence: Treated as part of the property file it’s defined in.
8. Programmatic Configuration (via Environment or ApplicationContext)
Inject and modify the Environment programmatically or use a custom PropertySource.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Properties;
@Component
public class CustomPropertyConfigurer {
@Autowired
private ConfigurableEnvironment environment;
@PostConstruct
public void init() {
Properties props = new Properties();
props.setProperty("spring.kafka.bootstrap-servers", "localhost:9092");
environment.getPropertySources().addFirst(new PropertiesPropertySource("customProps", props));
}
}
Precedence: Depends on where it’s added in the PropertySources stack (addFirst = highest).
9. @ConfigurationProperties
- Define a Java class to map properties with a specific prefix.
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "kafka")
public class KafkaProperties {
private String bootstrapServers;
public String getBootstrapServers() { return bootstrapServers; }
public void setBootstrapServers(String bootstrapServers) { this.bootstrapServers = bootstrapServers; }
}
Properties file:
kafka.bootstrap-servers=localhost:9092
Precedence: Tied to the underlying property source (e.g., application.properties).
10. @Value Annotation
- Inject individual properties directly into fields or parameters.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class KafkaConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
public String getBootstrapServers() { return bootstrapServers; }
}
- Precedence: Tied to the underlying property source.
11. Default Properties in Code
- Provide fallback values in @Value or @ConfigurationProperties.
@Value("${spring.kafka.bootstrap-servers:localhost:9092}")
private String bootstrapServers;
- Precedence: Lowest; overridden by any explicit property source.
12. Spring Boot Test Configuration
Use @TestPropertySource or inline properties in tests.
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@TestPropertySource(properties = "spring.kafka.bootstrap-servers=localhost:9999")
public class MyTest {
// Test code
}
Precedence: Applies only to the test context.
13. Dynamic Property Registry (Spring Boot Test)
Use DynamicPropertyRegistry in tests to set properties dynamically.
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
@TestConfiguration
public class TestConfig {
@Bean
public void dynamicProps(DynamicPropertyRegistry registry) {
registry.add("spring.kafka.bootstrap-servers", () -> "localhost:9092");
}
}
Precedence: Test-specific.
Precedence Order (Highest to Lowest)
- Command-line arguments
- Java system properties (-D)
- OS environment variables
- RandomValuePropertySource (random.*)
- External application.properties/application.yml (e.g., ./config/)
- Packaged application.properties/application.yml (classpath)
- Spring Cloud Config Server
- @TestPropertySource (in tests)
- Programmatic PropertySource additions
- Default values in code (@Value defaults)