Skip to content

Configuration — application.yml

application.yml is the central configuration file for a Spring Boot app. It lives in src/main/resources/ and tells Spring how to wire everything up at startup: database connection, server port, JPA behavior, logging, and more.

Spring Boot reads it automatically — no setup needed.


Database & JPA

spring:
  application:
    name: api-rest-sb

  datasource:
    url: jdbc:mysql://localhost:3306/api-rest-sb
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect
Property Description
spring.application.name Name of the app, used in logs and monitoring
datasource.url JDBC connection string
datasource.driver-class-name JDBC driver — must match your database dependency
jpa.hibernate.ddl-auto How Hibernate manages the schema on startup (see below)
jpa.show-sql Prints every SQL query Hibernate runs to the console
hibernate.dialect Tells Hibernate which SQL flavor to generate

ddl-auto values:

Value Behavior
update Applies schema changes without dropping data — use in dev
create-drop Creates schema on startup, drops it on shutdown — use in tests
validate Checks that the schema matches entities, fails if not — use in prod
none Does nothing — you manage the schema yourself

Server

server:
  port: 8080

Default is 8080. Change it if you have port conflicts or want a specific port in production.


Logging

logging:
  level:
    root: INFO
    com.example.myapp: DEBUG
    org.springframework.security: TRACE

You can set log levels globally (root) or per package. Useful for debugging a specific layer without drowning in logs from everything else.

Level Use
ERROR Only errors
WARN Errors + warnings
INFO General app lifecycle (default)
DEBUG Detailed flow — good for your own packages
TRACE Everything — very verbose, use for frameworks

H2 Database

H2 is an embedded Java database used for development and testing. It has two modes:

In-memory — data is lost on shutdown (default when H2 is on the classpath):

datasource:
  url: jdbc:h2:mem:testdb
  driver-class-name: org.h2.Driver

File-based — persisted to disk, survives restarts:

datasource:
  url: jdbc:h2:file:./data/mydb
  driver-class-name: org.h2.Driver

This creates a mydb.mv.db file at the specified path.

H2 is almost never used as a persistent database in real projects — use it for tests and local dev only. For local dev, prefer a real MySQL/PostgreSQL instance running in Docker so your environment matches production exactly.

Use case Database
Tests H2 in-memory (ddl-auto: create-drop)
Local dev MySQL / PostgreSQL in Docker
Production MySQL / PostgreSQL on a real server

If you add H2 to your classpath without any datasource config, Spring Boot auto-configures an in-memory datasource with defaults (sa / empty password, ddl-auto: create-drop). You don't need to configure anything until you want to override the defaults.


Profiles — application-{profile}.yml

You can create profile-specific config files that override application.yml when a profile is active. The most common use is swapping the database for tests.

Create src/main/resources/application-test.yml:

spring:
  application:
    name: api-rest-sb

  datasource:
    url: jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    driver-class-name: org.h2.Driver

  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect

  sql:
    init:
      mode: always
Property Description
jdbc:h2:mem:test In-memory H2 database named test
MODE=MySQL Makes H2 behave like MySQL (syntax compatibility)
DB_CLOSE_DELAY=-1 Keeps the DB alive as long as the JVM is running
ddl-auto: create-drop Fresh schema on every test run, dropped after
sql.init.mode: always Always runs data.sql to seed the database

Activate it in your test class with @ActiveProfiles("test") — see Testing.

You can also activate a profile globally by adding this to application.yml:

spring:
  profiles:
    active: test

Don't do this permanently — it's easy to forget and run your app against the test database. Prefer activating profiles per test class with @ActiveProfiles.


Custom Properties

You can define your own properties and inject them into any Spring bean with @Value:

app:
  jwt-secret: mysecretkey
  jwt-expiration: 86400000
@Service
public class JwtService {
    @Value("${app.jwt-secret}")
    private String secret;

    @Value("${app.jwt-expiration}")
    private long expiration;
}

Never commit secrets to your repository. Use environment variables or a secrets manager in production and reference them in the yaml: password: ${DB_PASSWORD}.