Spring Boot Tutorial
Application Properties and Common Settings
Spring Boot has thousands of configuration properties, but a handful appear in nearly every project: the server port, context path, application name, logging levels, database connection, JSON settings, file upload limits, and error handling. Knowing these — and knowing the order in which Spring Boot reads configuration — lets you change behaviour without touching code.
This lesson is a practical tour of the most common application.properties/application.yml settings, the property source order (command line, environment variables, profile files), relaxed binding, placeholders and random values, and how to find any property you need.
Where Properties Come From (Order of Precedence)
Spring Boot merges many property sources; a higher source overrides a lower one. From highest to lowest, the ones you use most are:
- Command-line arguments:
java -jar app.jar --server.port=9090. SPRING_APPLICATION_JSON(inline JSON in an environment variable).- OS environment variables:
SERVER_PORT=9090. - Java system properties:
-Dserver.port=9090(placed above environment variables in the order). - Profile-specific files outside the jar, then inside the jar:
application-prod.yml. - Application files outside the jar (
./config/, current directory), then inside the jar (classpath:application.yml). @PropertySourcefiles and default properties set in code.
Relaxed Binding and Environment Variables
Property names are matched flexibly. spring.datasource.url, SPRING_DATASOURCE_URL and spring.datasource.URL all bind to the same property. The environment-variable form — uppercase, dots replaced by underscores, dashes removed — is how containers and cloud platforms configure Spring Boot apps.
Commonly Used Properties
The settings you will change most often:
server.port(default 8080;0picks a random free port) andserver.servlet.context-path.spring.application.name— used in logs, metrics and service discovery.logging.level.<package>,logging.file.name.spring.datasource.*,spring.jpa.*— database and JPA.spring.jackson.*— JSON formatting.spring.servlet.multipart.max-file-size— upload limits.server.error.include-message,include-stacktrace— error detail.server.compression.enabled,server.ssl.*,server.shutdown.spring.profiles.active,spring.config.import.management.endpoints.web.exposure.include— Actuator.
Placeholders, Defaults and Random Values
Properties can reference other properties and environment variables with ${...}, including a default after a colon: ${DB_HOST:localhost}. ${random.uuid}, ${random.int(1000,9999)} generate values at startup. Keep secrets as references to environment variables, never as literal values in the file.
Finding Properties
The complete list lives in the Spring Boot reference appendix "Common Application Properties". IDEs (IntelliJ, VS Code with Spring tools) auto-complete property names and show documentation. At runtime, the /actuator/env and /actuator/configprops endpoints show effective values and where each came from (expose them only in secure, non-public environments).
Examples
A realistic application.yml
spring:
application:
name: webnest-shop
profiles:
active: ${SPRING_PROFILES_ACTIVE:dev}
datasource:
url: jdbc:postgresql://${DB_HOST:localhost}:5432/webnest
username: ${DB_USER:webnest}
password: ${DB_PASSWORD}
jpa:
open-in-view: false
servlet:
multipart:
max-file-size: 5MB
server:
port: 8081
servlet:
context-path: /shop
compression:
enabled: true
error:
include-message: always
include-stacktrace: never
logging:
level:
root: INFO
com.webnest: DEBUG
org.hibernate.SQL: DEBUG
file:
name: logs/webnest-shop.log
app:
instance-id: ${random.uuid}
Tomcat started on port 8081 (http) with context path '/shop'
curl http://localhost:8081/shop/api/products -> 200
Changing the port in five different ways
# 1. application.properties
server.port=9090
# 2. Command-line argument (highest precedence of these)
java -jar shop.jar --server.port=9091
# 3. Environment variable (containers, CI, cloud)
SERVER_PORT=9092 java -jar shop.jar # PowerShell: $env:SERVER_PORT=9092; java -jar shop.jar
# 4. Java system property
java -Dserver.port=9093 -jar shop.jar
# 5. Programmatically
@Bean
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> portCustomizer() {
return factory -> factory.setPort(9094);
}
# Random free port (useful for running several instances locally)
server.port=0
java -jar shop.jar --server.port=9091
... Tomcat started on port 9091 (http)
Overriding configuration per environment without rebuilding
# Packaged jar contains application.yml with server.port=8080
# ./config/application.yml next to the jar (overrides the packaged file)
server:
port: 8181
# Profile file for production
# application-prod.yml
logging:
level:
com.webnest: INFO
# Run
java -jar shop.jar --spring.profiles.active=prod
# Inspect where a value came from (Actuator, secured environments only)
curl localhost:8181/actuator/env/server.port
{"property":{"source":"Config resource 'file [config/application.yml]' via location 'optional:file:./config/'","value":"8181"}}
Common Mistakes
- Mixing application.properties and application.yml with the same keys and being confused which wins (properties files take precedence at the same location).
- Using tabs in YAML files, which breaks parsing; YAML requires spaces.
- Committing passwords as literal values instead of ${ENV_VAR} references.
- Setting server.servlet.context-path and forgetting to include it in client URLs and health-check paths.
- Exposing /actuator/env publicly, revealing configuration values.
Key Points to Remember
- Command-line args > environment variables > profile files > application files; outside the jar beats inside.
- Relaxed binding maps SERVER_PORT to server.port, so environment variables configure containers easily.
- server.port, context-path, spring.application.name, logging.level and datasource settings are the everyday properties.
- Use ${VAR:default} placeholders and keep secrets in the environment.
- Find properties in the reference appendix, IDE auto-completion and /actuator/env.
Practice the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Use your local JDK or project IDE for these examples. Codelab currently runs Python and HTML/CSS/JavaScript; framework examples may need project dependencies.