Spring Boot Basics: Build Your First Java Web App
Configuration and Profiles
In this lesson you will
- Set common properties in application.properties
- Override settings with environment variables and profiles
- Bind settings to a typed record
Introduction
An application behaves differently on your laptop, in testing and in production: different ports, databases and secrets. Spring Boot separates configuration from code. The property names in this lesson (such as server.port and spring.profiles.active) were checked in the metadata of the Spring Boot 4.1.2 release, but the code was not compiled or run here.
application.properties
The file src/main/resources/application.properties holds settings, one per line:
Properties
spring.application.name=library
server.port=8081
logging.level.com.example=DEBUGserver.portchanges the web server port (default 8080).logging.level.<package>sets the log level for a package.- You may use
application.ymlinstead, which uses indentation. Pick one style per project.
Where settings can come from
Spring Boot reads configuration from several places, with later, more specific sources overriding earlier ones. In practice, from weaker to stronger:
- Default values in code
application.propertiesin your jar- Profile-specific files such as
application-dev.properties - Environment variables
- Command-line arguments
This lets the same jar run anywhere, with settings supplied from outside. For example:
Terminal
SERVER_PORT=9090 java -jar app.jar
java -jar app.jar --server.port=9090Environment variables use relaxed binding: SERVER_PORT maps to server.port.
Profiles
A profile is a named set of configuration, such as dev, test and prod.
Create application-dev.properties:
Properties
logging.level.com.example=DEBUG
spring.h2.console.enabled=trueand application-prod.properties:
Properties
logging.level.com.example=INFO
server.shutdown=gracefulActivate a profile:
Properties
spring.profiles.active=devor with an environment variable: SPRING_PROFILES_ACTIVE=prod. Settings in the active profile file override the main file.
You can also make beans appear only in some profiles:
Java
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("dev")
public class DevDataLoader { /* creates sample data in dev only */ }Quick quiz
How can you change the server port without editing the jar?
Reading single values
Java
import org.springframework.beans.factory.annotation.Value;
@Value("${app.greeting:Hello}")
private String greeting;${app.greeting:Hello} reads app.greeting and uses Hello if it is missing. It works, but scattered @Value fields are hard to maintain.
Typed configuration
Group related settings in a record and let Spring bind and validate them.
Java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@ConfigurationProperties(prefix = "library")
public record LibraryProperties(String name, int maxLoans) {}Enable it on a configuration class or the main class:
Java
@SpringBootApplication
@EnableConfigurationProperties(LibraryProperties.class)
public class DemoApplication { /* ... */ }with properties:
Properties
library.name=City Library
library.max-loans=5Property names written in kebab case (max-loans) bind to the Java name maxLoans. You can then inject LibraryProperties into any class that needs it.
Secrets
- Never commit passwords, API keys or tokens in properties files.
- Supply them through environment variables or a secrets manager.
- Add local files that contain secrets to
.gitignore. - Keep a template with fake values for teammates.
Try it yourself
Add application-dev.properties and application-prod.properties with different log levels. Run the app with each profile active and compare the logs. Bind a LibraryProperties record and show its values from an endpoint.
Summary
- Settings live in application.properties, and environment variables and arguments override them.
- Profiles hold environment-specific settings and are activated with spring.profiles.active.
- Use @ConfigurationProperties for typed settings, and never commit secrets.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading