Skip to content
Visual explainer 4h to learn 4 steps

Spring Boot: starters, config & profiles

Configure an app for dev, test and prod.

By Invicly Tech, , in Backend Java Development. About 10 minutes to read.

Topic 16 of 29

In short

Spring Boot adds starters, auto-configuration and an embedded server so an app runs with almost no setup. Profiles and external config let one build behave correctly in dev, test and prod.

Explained simply

Plain Spring can do almost anything, but setting it up used to take lots of configuration. Spring Boot is a layer on top that makes sensible choices for you, so a working web app with a database needs only a few lines of your own code.

Boot has three big ideas. Starters are dependency bundles: add spring-boot-starter-web and you get Spring MVC, JSON support and an embedded Tomcat server. Auto-configuration creates the beans those libraries need. And the embedded server means your app is a single jar you run with java -jar.

Configuration lives outside your code, in application.yml or application.properties, and can be overridden by environment variables or command-line flags. The same jar can therefore run on your laptop, in a test pipeline and in production, each with its own settings.

Profiles group settings by environment. A dev profile might use a local database and verbose logging; prod uses the real database and quiet logs. You choose the profile when starting the app, without rebuilding anything.

Press Play on a diagram to walk through it one step at a time.

FlowWhat happens at startup
  1. 1
    SpringApplication.run

    Entry point in the @SpringBootApplication class

  2. 2
    Load config

    application.yml plus the active profile's file

  3. 3
    Auto-configuration

    Creates beans based on classpath and properties

  4. 4
    Create your beans

    Component scan finds your classes

  5. 5
    Start embedded server

    Tomcat listens on server.port

LayersConfig precedence (highest first)
  1. 1
    Command-line arguments

    --server.port=9090

  2. 2
    Environment variables

    SERVER_PORT=9090, typical in containers

  3. 3
    Profile config file

    application-prod.yml when prod is active

  4. 4
    application.yml

    Shared defaults committed with the code

MapWhat @SpringBootApplication contains
@SpringBootApplication
  • @SpringBootConfiguration
    • Marks the main configuration class
  • @EnableAutoConfiguration
    • Loads conditional auto-configurations
    • Backs off when you define beans
  • @ComponentScan
    • Scans this package and below
    • Finds @Component, @Service, @Controller

In depth

Starting a project with Spring Initializr

start.spring.io generates a ready-to-build project. You choose the build tool, the Java version, the Spring Boot version, your group and artifact names, and the dependencies, such as Spring Web, Spring Data JPA, PostgreSQL Driver and Validation. Most IDEs offer the same wizard. You can even script it: curl https://start.spring.io/starter.zip -d dependencies=web,data-jpa,postgresql -d javaVersion=21 -o demo.zip.

The generated project has the standard layout, a main class annotated with @SpringBootApplication, an empty application.properties, a test that checks the context starts, and the build wrapper. Building with ./mvnw package produces an executable fat jar in target/ that contains your code, every dependency and a small launcher, so the server needs nothing but a Java runtime.

  • ./mvnw spring-boot:run starts the app from source
  • java -jar target/demo-0.0.1-SNAPSHOT.jar runs the packaged jar
  • Pick the latest stable Boot version and a supported LTS Java

How auto-configuration decides

@SpringBootApplication combines three annotations: @SpringBootConfiguration, @ComponentScan and @EnableAutoConfiguration. The last one loads a long list of auto-configuration classes shipped inside Boot's jars. Each one is guarded by conditions. @ConditionalOnClass means only if a library is on the classpath. @ConditionalOnMissingBean means only if you have not defined your own bean of that type. @ConditionalOnProperty means only if a setting is present or has a certain value.

That is why defining your own DataSource or ObjectMapper bean makes Boot's version back off. To see every decision, start the app with --debug: Boot prints a conditions evaluation report listing which auto-configurations matched and why others did not. To switch one off, use @SpringBootApplication(exclude = ...).

FlowShould Boot create a DataSource?
  1. 1
    JDBC classes present?

    No: skip. Yes: continue

  2. 2
    Your own DataSource bean?

    Yes: back off and use yours

  3. 3
    Read spring.datasource.*

    URL, username, password, pool settings

  4. 4
    Pick a pool

    HikariCP when it is on the classpath

  5. 5
    Register the bean

    JPA and JdbcTemplate can now use it

Properties, environment variables and typed config

Boot uses relaxed binding, so one setting can be written several ways. spring.datasource.url in YAML, and the environment variable SPRING_DATASOURCE_URL, mean the same thing: upper-case it, replace dots with underscores, and drop dashes. This is how containers and Kubernetes configure Spring apps without changing files.

For your own settings, prefer @ConfigurationProperties over many scattered @Value annotations. Bind a prefix such as app.mail to a record with typed fields; Boot converts strings like 5s into a Duration and 10MB into a DataSize. Add @Validated and Bean Validation annotations so the app refuses to start with a missing or invalid value, which is much better than failing at 3 a.m. on the first email. spring.config.import can load extra files, for example optional:file:./secrets.yml.

  • server.port=8081 or SERVER_PORT=8081
  • @Value("${app.name}") for a single value
  • @ConfigurationPropertiesScan finds properties records

Profiles in practice

A profile is a name such as dev, test or prod. When a profile is active, Boot also loads application-dev.yml (or a document inside application.yml marked with spring.config.activate.on-profile). Values there override the shared defaults. Activate profiles with spring.profiles.active, the SPRING_PROFILES_ACTIVE environment variable, or @ActiveProfiles("test") in tests. If none is set, the profile named default is active.

Profiles can also switch beans: @Profile("dev") on a FakeEmailSender means it exists only in dev. Profile groups bundle several: spring.profiles.group.prod=prod-db,prod-mail. Keep profiles few and meaningful. A common mistake is putting real secrets into application-prod.yml in Git; keep only non-secret settings there and inject secrets through environment variables or a secret store. The startup log always names the active profiles, so check it first when config looks wrong.

MapWhich files load with SPRING_PROFILES_ACTIVE=prod
src/main/resources
  • application.yml
    • Always loaded
    • Shared defaults
  • application-prod.yml
    • Loaded, overrides defaults
    • Non-secret prod settings
  • application-dev.yml
    • Not loaded
    • Only when dev is active
  • Environment variables
    • Override both files
    • Secrets such as DB passwords

Developer comfort and production defaults

spring-boot-devtools restarts the app automatically when classes change and disables template caching during development; it is switched off automatically when running a packaged jar. The spring-boot-docker-compose module (Boot 3.1+) can start the services in your compose.yaml when the app starts and wire their connection details, so a local Postgres needs no manual config.

For production, Boot has sensible defaults. The embedded server is Tomcat on port 8080. Graceful shutdown lets in-flight requests finish when the app is stopped; it is the default since Boot 3.4 and tunable with spring.lifecycle.timeout-per-shutdown-phase. Logging goes to the console, which suits containers. Add the Actuator starter to get health checks and metrics, covered later in this path.

Learn it in 4 short steps

About 1h each. Track them with a timer in your plan.

  1. 1
    Create a project with Spring Initializr~1h
  2. 2
    Learn starters and auto-configuration~1h
  3. 3
    Configure with application.yml~1h
  4. 4
    Set up dev, test and prod profiles~1h

Key points

  • A starter like spring-boot-starter-web pulls in a tested set of dependencies.
  • Auto-configuration backs off when you define your own bean of the same type.
  • Activate profiles with spring.profiles.active or SPRING_PROFILES_ACTIVE.
  • Keep secrets out of the repo; inject them through environment variables.

Key terms

Tap a card to flip it. 0/5 known

Examples

yaml
# application.yml
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/app
    username: app
server:
  port: 8080
---
# Only when the prod profile is active
spring:
  config:
    activate:
      on-profile: prod
  datasource:
    url: ${DB_URL}

Run with java -jar app.jar --spring.profiles.active=prod

Typed, validated configuration with a record

Your app sends emails and needs a sender address, a timeout and an on/off switch. Step 1: declare a record bound to the app.mail prefix. Step 2: add @Validated and @NotBlank so startup fails if the sender is missing. Step 3: use @DefaultValue for the timeout; Boot converts 5s into a Duration. Step 4: enable scanning on the main class. Step 5: inject MailProperties anywhere like a normal bean. In Kubernetes you would set APP_MAIL_FROM as an environment variable.

java
@ConfigurationProperties(prefix = "app.mail")
@Validated
public record MailProperties(
    @NotBlank String from,
    @DefaultValue("5s") Duration timeout,
    boolean enabled) {}

@SpringBootApplication
@ConfigurationPropertiesScan
public class App {
  public static void main(String[] args) { SpringApplication.run(App.class, args); }
}

// application.yml
// app.mail.from: noreply@shop.example
// app.mail.enabled: true

Dev and prod settings from one jar

Step 1: shared settings go in application.yml. Step 2: application-dev.yml points at a local database and turns on SQL logging. Step 3: application-prod.yml sets production behaviour but reads the database URL and password from environment variables, so no secret is in Git. Step 4: locally, run with the dev profile; in the container, set SPRING_PROFILES_ACTIVE=prod plus the variables. Step 5: confirm in the startup log line that names the active profile.

yaml
# application-dev.yml
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/shop
    username: shop
    password: shop
  jpa:
    show-sql: true

# application-prod.yml
spring:
  datasource:
    url: ${DB_URL}
    username: ${DB_USER}
    password: ${DB_PASSWORD}
# Run: SPRING_PROFILES_ACTIVE=prod DB_URL=... java -jar app.jar

Quick reference

  • spring.datasource.url can be set as the environment variable SPRING_DATASOURCE_URL
  • --debug prints the conditions report: which auto-configurations matched and why
  • Define your own bean to replace Boot's: @ConditionalOnMissingBean backs off
  • application-{profile}.yml overrides application.yml when that profile is active
  • SPRING_PROFILES_ACTIVE=prod or --spring.profiles.active=prod picks the profile
  • @ConfigurationProperties records give typed, validated settings
  • Durations and sizes bind from text: 30s, 5m, 10MB
  • Keep secrets in environment variables or a secret store, never in Git

Common mistakes

  • Committing production passwords to application.yml.
  • Not knowing which profile is active; check the startup log line.
  • Fighting auto-configuration instead of overriding a single property or bean.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1What environment variable sets spring.datasource.username?

Question 2

2You define your own ObjectMapper bean. What does Boot's Jackson auto-configuration do?

Question 3

3How can you find out why an auto-configuration did not apply?

Question 4

4No profile is set. Which profile is active?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Generate a Spring Boot web project from the command line, run it on port 9090 instead of 8080, and check it responds.

  2. Exercise 2medium

    Bind settings app.upload.max-size (default 10MB) and app.upload.dir (required) to a record and print them at startup.

  3. Exercise 3hard

    Use a fake email sender in the dev profile and a real SMTP sender in all other profiles, and prove which one is active.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use application.yml or application.properties?
Both work the same. YAML is easier to read for nested settings; properties files are simpler to grep. Pick one per project and do not mix them for the same keys.
How do I set the profile in Docker or Kubernetes?
Set the environment variable SPRING_PROFILES_ACTIVE=prod in the container definition. Other settings follow the same pattern, such as SPRING_DATASOURCE_URL.
What is the difference between Spring and Spring Boot?
Spring Framework provides the container, MVC, data access and more. Spring Boot is built on top and adds starters, auto-configuration, an embedded server and production features, so you configure far less.
Which Spring Boot version should I use?
Use the latest stable release from start.spring.io. Spring Boot 3 needs Java 17 or newer, and Boot 4 (released in late 2025) is built on Spring Framework 7.

Go deeper

ShareXLinkedInWhatsApp