Spring Boot Introduction
BeginnerSpring Boot is an opinionated framework that eliminates boilerplate Spring configuration through auto-configuration, embedded servers, and production-ready defaults.
Overview
Spring Boot is built on top of the Spring Framework and takes an opinionated view of application development. Its three pillars are: (1) Auto-configuration — Spring Boot inspects the classpath and beans present and automatically configures the application (e.g. if spring-boot-starter-web is present, it configures DispatcherServlet, Jackson, and an embedded Tomcat); (2) Embedded servers — Tomcat, Jetty, or Undertow are packaged into the JAR, eliminating the need to deploy WAR files to an external container; (3) Starter dependencies — curated POMs bundle compatible versions of related libraries. The result is a runnable, self-contained JAR with java -jar.
Creating a Spring Boot Application
The entry point is a class annotated with @SpringBootApplication, which combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. SpringApplication.run() bootstraps the application context, starts the embedded server, and fires application events.
@SpringBootApplication // = @Configuration + @EnableAutoConfiguration + @ComponentScan
public class AiCanCodeApplication {
public static void main(String[] args) {
SpringApplication.run(AiCanCodeApplication.class, args);
}
}
// application.properties — key defaults you often override
server.port=8080
spring.application.name=toolhub
spring.profiles.active=dev
// Fat JAR build + run
// mvn package → target/toolhub-1.0.0.jar
// java -jar target/toolhub-1.0.0.jar --server.port=9090Auto-Configuration Explained
Auto-configuration classes live in spring-boot-autoconfigure.jar and are listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each is conditionally activated with @ConditionalOnClass, @ConditionalOnMissingBean, etc.
// How to see what auto-configuration is applied
// 1. Run with --debug flag
java -jar app.jar --debug
// Prints a "CONDITIONS EVALUATION REPORT" with matched and unmatched conditions
// 2. Actuator endpoint
// GET /actuator/conditions (requires spring-boot-starter-actuator)
// Example: DataSource auto-configuration (simplified)
@Configuration
@ConditionalOnClass(DataSource.class) // only if JDBC driver on classpath
@ConditionalOnMissingBean(DataSource.class) // only if no custom DataSource bean
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
@Bean
public DataSource dataSource(DataSourceProperties props) {
return props.initializeDataSourceBuilder().build();
}
}
// Override auto-configured DataSource by declaring your own @Bean
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:postgresql://...");
return ds; // auto-config backs off due to @ConditionalOnMissingBean
}Application Properties & Profiles
application.properties (or .yml) is the central config file. Profile-specific files (application-prod.properties) override defaults when the profile is active. Properties can be overridden via environment variables, command-line args, or @TestPropertySource in tests.
# application.yml — base config
spring:
application:
name: toolhub
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
---
# application-prod.yml — overrides when spring.profiles.active=prod
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:postgresql://prod-db:5432/toolhub
username: ${DB_USER}
password: ${DB_PASS}
# Override any property at startup:
java -jar app.jar --spring.datasource.url=jdbc:postgresql://override-db:5432/toolhub
# Or via env var (Spring relaxed binding):
export SPRING_DATASOURCE_URL=jdbc:postgresql://...Key Points to Remember
- 1@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan.
- 2Auto-configuration is conditional — it backs off when you define your own beans.
- 3Embedded Tomcat/Jetty means the app runs as a plain JAR with java -jar.
- 4application.properties/yml is the central config; profile files override per environment.
- 5Property override order: command-line args > env vars > profile files > application.properties.
- 6Use --debug or /actuator/conditions to inspect which auto-configurations are active.
Interview Questions
Sign in to ask AriaWhat does @SpringBootApplication do under the hood?
How does Spring Boot auto-configuration work and how can you disable it for a specific class?
What is the property override order in Spring Boot?
How would you debug which auto-configurations are active in a Spring Boot app?
Explain the difference between @SpringBootApplication and @EnableAutoConfiguration.
Ask Aria about Spring Boot Introduction
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.