External configuration means keeping settings such as application names, URLs, ports, feature flags, credentials, or environment-specific values outside your Java code.
Spring supports this mainly through:
application.propertiesapplication.yml- environment variables
- command-line arguments
- external property files
@Value@ConfigurationProperties
1. Using application.properties
In a Spring or Spring Boot application, you can place configuration in:
src/main/resources/application.properties
Example:
app.name=My Spring App
app.version=1.0.0
app.description=Example application using external configuration
Then inject values with @Value:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppProperties {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
@Value("${app.description}")
private String appDescription;
public void printProperties() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
System.out.println("App Description: " + appDescription);
}
}
2. Using application.yml
You can also use YAML:
app:
name: My Spring App
version: 1.0.0
description: Example application using external configuration
The same @Value expressions still work:
@Value("${app.name}")
private String appName;
3. Using @PropertySource in non-Boot Spring
If you are using plain Spring with Java configuration, register a property file using @PropertySource:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.example.app")
@PropertySource("classpath:application.properties")
public class AppConfig {
}
Then Spring can resolve values like:
@Value("${app.name}")
private String appName;
If you are using Spring Boot, you usually do not need @PropertySource for application.properties or application.yml. Spring Boot loads them automatically.
4. Recommended Spring Boot Approach: @ConfigurationProperties
For multiple related properties, prefer @ConfigurationProperties over many @Value fields.
Example application.properties:
app.name=My Spring App
app.version=1.0.0
app.description=Example application using external configuration
Create a properties class:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String version;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Then inject it into another bean:
import org.springframework.stereotype.Service;
@Service
public class GreetingService {
private final AppProperties appProperties;
public GreetingService(AppProperties appProperties) {
this.appProperties = appProperties;
}
public void printGreeting() {
System.out.println("Welcome to " + appProperties.getName());
System.out.println("Version: " + appProperties.getVersion());
System.out.println(appProperties.getDescription());
}
}
5. External Files Outside the JAR
You can override configuration from outside the application.
For Spring Boot:
java -jar myapp.jar --spring.config.location=file:/opt/myapp/application.properties
Or include an additional config file:
java -jar myapp.jar --spring.config.additional-location=file:/opt/myapp/
Example external file:
app.name=Production App
app.version=2.0.0
app.description=Running with production configuration
6. Environment Variables
Spring Boot can read environment variables automatically.
For example, this property:
app.name=My Spring App
Can be overridden with:
APP_NAME=Production App
Spring Boot maps environment variable names to property names using relaxed binding:
APP_NAME -> app.name
SERVER_PORT -> server.port
SPRING_DATASOURCE_URL -> spring.datasource.url
7. Command-Line Arguments
You can override properties when starting the application:
java -jar myapp.jar --app.name="Command Line App" --server.port=9090
Command-line arguments usually have high priority and override values from property files.
8. Profiles for Environment-Specific Config
Profiles let you separate configuration by environment.
Common files:
application.properties
application-dev.properties
application-test.properties
application-prod.properties
Example:
# application-prod.properties
app.name=Production App
server.port=8080
Run with a profile:
java -jar myapp.jar --spring.profiles.active=prod
Or set an environment variable:
SPRING_PROFILES_ACTIVE=prod
9. Default Values with @Value
You can provide fallback values:
@Value("${app.name:Default App}")
private String appName;
If app.name is missing, Spring uses "Default App".
10. Common Property Examples
server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=secret
logging.level.org.springframework=INFO
app.name=My Spring App
app.version=1.0.0
Summary
Use external configuration like this:
| Use case | Recommended approach |
|---|---|
| Simple single value | @Value("${property.name}") |
| Group of related settings | @ConfigurationProperties |
| Spring Boot default config | application.properties or application.yml |
| Plain Spring Java config | @PropertySource |
| Environment-specific config | Spring profiles |
| Production overrides | external file, environment variables, or command-line args |
For most Spring Boot applications, the usual setup is:
src/main/resources/application.properties
plus a configuration class using:
@ConfigurationProperties(prefix = "app")
This keeps configuration clean, type-safe, and easy to override per environment.
