Spring Boot
What is Spring Boot
Spring Boot is an opinionated, convention-over-configuration extension of Spring Framework that eliminates boilerplate setup. It auto-configures your application based on what's on the classpath, embeds a web server, and gets a production-ready Spring application running in minutes instead of days.
The Problem Spring Boot Solved
Spring Framework is powerful, but setting it up was notoriously painful. A new Spring MVC web application required:
- Writing multiple XML configuration files (web.xml, applicationContext.xml, dispatcher-servlet.xml)
- Manually declaring every bean, datasource, transaction manager, and view resolver
- Downloading and configuring an external application server (Tomcat, JBoss)
- Manually managing dozens of dependency versions that all needed to be compatible
- Deploying a WAR file to the server
An experienced developer could spend a full day just setting up a new project before writing a single line of business logic. And version conflicts between dependencies were a constant source of mysterious failures.
Spring Boot, released in 2014 by Pivotal, attacked this problem directly. Its goal: get a Spring application running with zero configuration, sensible defaults, and an embedded server — so developers can focus on the application, not the infrastructure.
What Spring Boot Actually Does
Spring Boot sits on top of Spring Framework and provides three core capabilities:
Auto-configuration — Spring Boot examines your classpath and automatically configures Spring beans based on what it finds. Add spring-data-jpa to your dependencies? Spring Boot automatically configures a DataSource, EntityManagerFactory, and TransactionManager. Add spring-security? An authentication layer is configured automatically. You can override any default, but you rarely need to.
Starter dependencies — Instead of hunting down 10 compatible libraries for a web application, you add one starter: spring-boot-starter-web. It pulls in Spring MVC, Jackson (JSON), an embedded Tomcat server, and all their transitive dependencies at compatible versions.
Embedded server — Spring Boot embeds Tomcat (or Jetty, or Undertow) directly in your application. Your app is a self-contained JAR that you run with java -jar. No external server needed. Deploy anywhere Java runs.
A Spring Boot Application
Here's the entirety of what you need to run a Spring Boot web application — one class:
Java
// The entire Spring Boot application — one file:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
// @SpringBootApplication is a convenience annotation combining:
// @Configuration — this class can define Spring beans
// @EnableAutoConfiguration — trigger Spring Boot's auto-configuration
// @ComponentScan — scan this package for Spring components
// Add a REST endpoint — zero additional configuration:
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}
// Run it:
// mvn spring-boot:run
// OR: java -jar myapp.jar
// Visit: http://localhost:8080/hello
// Visit: http://localhost:8080/hello?name=AliceThe @SpringBootApplication Annotation
@SpringBootApplication is the entry point annotation that activates Spring Boot's core features. Understanding what it does under the hood removes the magic.
Java
// @SpringBootApplication expands to these three annotations:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MyApplication { }
// @Configuration — marks this as a configuration class:
// Spring can use it to define @Bean methods
@Configuration
public class MyConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
// @EnableAutoConfiguration — the key Spring Boot annotation:
// Spring Boot scans spring.factories (or AutoConfiguration.imports in Boot 3+)
// for hundreds of auto-configuration classes and applies those that match
// the classpath and existing beans
// @ComponentScan — scans the package and sub-packages for:
// @Component, @Service, @Repository, @Controller, @RestController
// and registers them as Spring beans automatically
// Important: put @SpringBootApplication in the ROOT package:
// com.example.myapp.MyApplication ← scans com.example.myapp.*
// If placed inside a sub-package, sibling packages won't be scannedAuto-Configuration in Practice
Auto-configuration is what makes Spring Boot feel magical. Add a dependency, and Spring Boot configures it. Here's what actually happens:
Java
// Example: add spring-boot-starter-data-jpa to pom.xml
// Spring Boot automatically configures:
// - DataSource (connection pool using HikariCP by default)
// - JPA EntityManagerFactory
// - Spring Data JPA repositories
// - Transaction management
// All you need to do is set the connection details in application.properties:
// spring.datasource.url=jdbc:mysql://localhost:3306/mydb
// spring.datasource.username=root
// spring.datasource.password=secret
// Then write your repository — zero config needed:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
List<User> findByActiveTrue();
}
// Spring Boot creates the implementation automatically at startup.
// No XML. No @Bean configuration for EntityManagerFactory. Nothing.
// See what was auto-configured:
// Add --debug flag when running: java -jar app.jar --debug
// Or in application.properties: debug=true
// Spring Boot prints every auto-configuration it applied and why
// Override any auto-configured bean by defining your own:
@Configuration
public class DataSourceConfig {
@Bean
@Primary
public DataSource dataSource() {
// Your custom DataSource — overrides Spring Boot's auto-configured one
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/mydb")
.username("root")
.password("secret")
.build();
}
}application.properties — The Configuration File
Spring Boot applications are configured through application.properties (or application.yml). Hundreds of properties control every aspect of auto-configuration — datasource, server port, logging, security, and more.
application.properties
# Server configuration:
server.port=8080
server.servlet.context-path=/api
# Database:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secret
spring.datasource.driver-class-name=org.postgresql.Driver
# JPA/Hibernate:
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
# Connection pool (HikariCP):
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
# Logging:
logging.level.root=INFO
logging.level.com.example=DEBUG
logging.level.org.springframework.web=DEBUG
logging.file.name=logs/application.log
# Custom application properties:
app.jwt.secret=mysecretkey
app.jwt.expiration=86400000
app.mail.from=noreply@example.comRunning and Deploying Spring Boot Applications
Spring Boot's embedded server makes running and deploying applications dramatically simpler than traditional Java web apps.
Shell
# Development — run directly:
mvn spring-boot:run
# or:
./gradlew bootRun
# Build a self-contained JAR (fat JAR — includes all dependencies):
mvn clean package
# Produces: target/myapp-1.0.0.jar (typically 20-50 MB — contains everything)
# Run the JAR — works on any machine with Java installed:
java -jar target/myapp-1.0.0.jar
# Override properties at runtime:
java -jar myapp.jar --server.port=9090
java -jar myapp.jar --spring.profiles.active=production
# Environment-specific config — application-{profile}.properties:
# application-dev.properties — used when profile is 'dev'
# application-prod.properties — used when profile is 'prod'
java -jar myapp.jar --spring.profiles.active=prod
# Docker — one Dockerfile for any Spring Boot app:
# FROM eclipse-temurin:21-jre
# COPY target/myapp.jar app.jar
# ENTRYPOINT ["java", "-jar", "/app.jar"]
# Spring Boot Maven plugin also creates layered JARs for efficient Docker caching:
mvn spring-boot:build-image # builds a Docker image directly, no Dockerfile needed