Spring
02 / 05

Spring Boot & Auto-configuration

Spring Boot & Auto-configuration

Spring Boot eliminates boilerplate by auto-configuring Spring based on classpath dependencies. Opinionated defaults you can override with application.properties / application.yml.

Project Setup

# start.spring.io — fastest way to bootstrap
curl https://start.spring.io/starter.zip   -d dependencies=web,data-jpa,postgresql,security,validation,actuator   -d type=maven-project   -d language=java   -d bootVersion=3.3.0   -d groupId=com.example   -d artifactId=myapp   -o myapp.zip && unzip myapp.zip

# Or use Spring Initializr IntelliJ plugin / VS Code Spring Boot extension

# Maven commands
./mvnw spring-boot:run        # run in development
./mvnw test                   # run tests
./mvnw package                # build JAR
java -jar target/myapp-0.0.1-SNAPSHOT.jar

application.yml Configuration

server:
  port: 8080
  servlet:
    context-path: /api

spring:
  application:
    name: my-app

  datasource:
    url: jdbc:postgresql://localhost:5432/mydb
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    hikari:
      maximum-pool-size: 20
      connection-timeout: 30000

  jpa:
    hibernate:
      ddl-auto: validate        # validate | update | create | create-drop | none
    show-sql: false
    open-in-view: false         # disable OSIV — prevents N+1 in web layer

  data:
    redis:
      host: localhost
      port: 6379

logging:
  level:
    com.example: DEBUG
    org.springframework.security: INFO

# Custom properties (type-safe with @ConfigurationProperties)
app:
  jwt:
    secret: ${JWT_SECRET}
    expiry-minutes: 60
  cors:
    allowed-origins: https://app.example.com

Type-Safe Configuration

// Bind application.yml properties to a class
@ConfigurationProperties(prefix = "app.jwt")
@Validated
public record JwtProperties(
    @NotBlank String secret,
    @Positive int expiryMinutes
) {}

// Enable in main class or @Configuration
@SpringBootApplication
@ConfigurationPropertiesScan
public class MyAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyAppApplication.class, args);
    }
}

// Inject like any other bean
@Service
public class TokenService {
    public TokenService(JwtProperties jwt) {
        this.secret = jwt.secret();
    }
}

Actuator & Profiles

# application.yml — Actuator endpoints for monitoring
management:
  endpoints:
    web:
      exposure:
        include: health, info, metrics, prometheus
  endpoint:
    health:
      show-details: when-authorized
  metrics:
    export:
      prometheus:
        enabled: true

# application-dev.yml — development overrides
spring:
  jpa:
    show-sql: true
  h2:
    console:
      enabled: true

logging:
  level:
    root: DEBUG
# Activate profiles
SPRING_PROFILES_ACTIVE=production java -jar app.jar
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev,local

# Build info in /actuator/info
# Add to pom.xml: spring-boot-maven-plugin with build-info goal

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free