The internal flow of Spring Boot refers to what happens behind the scenes when you run a Spring Boot application — from startup to handling requests.
🔁 1. Application Startup (main()
method)
@SpringBootApplication
When you run a Spring Boot application using the entry point(SpringApplication.run(...)) then bootstraps the entire Spring Boot application
-
And creates a
SpringApplication
object. -
And sets up:
-
Environment (like
application.properties
) -
ApplicationContext (Spring Container)
-
Auto-configuration
-
Logging system
-
Banner display
-
Listeners and initializers
-
🔧 2. Component Scan & Bean Creation
Triggered by:
Internals:
-
And ComponentScan internally scans all classes under the base package and sub-packages.
-
Registers beans for:
-
@Component
,@Service
,@Repository
,@Controller
,@RestController
-
-
Spring’s
BeanFactory
orApplicationContext
creates and manages the lifecycle of these beans. -
Dependency injection using
@Autowired
,@Value
,@Qualifier
, etc.
⚙️ 3. Auto Configuration
Provided by:
Internals:
-
Uses
spring.factories
to load classes annotated with@Configuration
. -
Auto-configures beans (like DataSource, Kafka, MVC, etc.) based on classpath and properties.
-
Uses conditional annotations like:
-
@ConditionalOnClass
-
@ConditionalOnMissingBean
-
@ConditionalOnProperty
-
🛠 4. Initialization of Embedded Server
Example:
-
Tomcat (default), Jetty, or Undertow
Internals:
-
Spring Boot auto-configures and starts the embedded server.
-
DispatcherServlet
is initialized and mapped to handle all requests (/
). -
Servlet context is set up.
🌐 5. Request Handling (Web Layer)
Steps:
-
HTTP request hits the embedded server (Tomcat).
-
Routed to the
DispatcherServlet
. -
DispatcherServlet uses
HandlerMapping
to find the correct controller method. -
Executes:
-
HandlerInterceptors
-
Controller method (e.g.,
@GetMapping
,@PostMapping
)
-
-
Returns the result (ResponseEntity, JSON, View).
-
DispatcherServlet sends response back to the client.
📦 6. Persistence Layer (If Configured)
-
Auto-configures:
-
DataSource (JDBC or JPA)
-
Hibernate EntityManager
-
Repositories via
@Repository
and@EnableJpaRepositories
-
-
Transaction management via
@Transactional
📊 7. Actuator & Monitoring (Optional)
-
Spring Boot Actuator auto-configures endpoints for:
-
/actuator/health
-
/actuator/info
-
/actuator/metrics
-
-
Useful for health checks and integration with Prometheus/Grafana
🛑 8. Shutdown Flow
-
Triggers graceful shutdown:
-
Calls
SmartLifecycle.stop()
-
Closes ApplicationContext
-
Releases resources (e.g., DB connections, Kafka consumers)
-
-
Beans with
@PreDestroy
or implementingDisposableBean
are cleaned up.
⚙️ Internal Components Summary
Component | Role |
---|---|
SpringApplication | Bootstraps and configures the application |
ApplicationContext | Manages beans and dependency injection |
DispatcherServlet | Front controller for web requests |
BeanPostProcessor | Allows custom modification of beans |
@Configuration | Declares Spring configurations |
@EnableAutoConfiguration | Enables Spring Boot’s intelligent auto setup |
CommandLineRunner / ApplicationRunner | Executes code after startup |
🔍 Spring Boot Internal Flow
✅ Visual Diagram + Call Stack Trace
📌 A. Visual Architecture Diagram of SpringApplication.run(...)
📌 B. Internal Call Stack Trace
(Important Classes and Methods Behind the Scenes)
📦 Summary Diagram:
✅ Key Classes Involved:
Component | Responsibility |
---|---|
SpringApplication | Entry point for bootstrapping the app |
ConfigurableApplicationContext | Spring’s container holding all beans |
ApplicationContextInitializer | Modify context before refresh |
BeanFactoryPostProcessor | Modify BeanFactory before beans instantiated |
BeanPostProcessor | Modify beans after instantiation |
AnnotationConfigServletWeb...Ctx | IoC + embedded web server (Tomcat) |
@SpringBootApplication | Combo of @Configuration , @ComponentScan , @EnableAutoConfiguration |
ApplicationRunner , CommandLineRunner | Code to run just before app is ready |
📌 Optional: View Actual SpringApplication.run()
Flow in Debug Mode
Set a breakpoint in your IDE:
And then step into to trace each class mentioned above.
SpringApplication class in Spring Boot
✅ SpringApplication is a class in Spring Boot that is responsible for bootstrapping and launching a Spring application from the main()
method. It sets up the Spring ApplicationContext, performs auto-configuration, and starts the embedded server (like Tomcat).
📌 Simple Definition:
SpringApplication
is the entry point used to start a Spring Boot application.
🧱 Syntax:
🔧 Example:
🔍 What It Does Internally:
-
Creates an
ApplicationContext
-
Loads beans and applies auto-configuration
-
Sets up logging, environment, and banner
-
Starts embedded web server (Tomcat/Jetty/Undertow)
-
Runs any
CommandLineRunner
orApplicationRunner
beans
🔩 Advanced Usage:
📚 Key Features:
-
Auto-configuration
-
Environment loading (application.properties/yaml)
-
Command line argument parsing
-
Banner control
-
Listener and event publishing support
---------------------------
✅ 2. Spring Framework Internal Flow
🌱 Spring Core Container Flow
-
IoC Container (Inversion of Control):
-
Reads configuration (
@Configuration
,@Component
) -
Instantiates beans
-
Resolves dependencies via:
-
@Autowired
,@Qualifier
,@Primary
, XML, etc.
-
-
🔄 Bean Lifecycle:
-
Instantiation
-
Populate properties (
@Autowired
, setters) -
BeanNameAware
,BeanFactoryAware
callbacks -
InitializingBean.afterPropertiesSet()
or@PostConstruct
-
Bean is ready to use
-
On shutdown:
@PreDestroy
orDisposableBean.destroy()
✅ 3. Spring MVC Internal Flow
🌐 HTTP Request Flow (Spring Web MVC DispatcherServlet)
🔁 Step-by-step Request Flow:
🔹 1. Client Sends HTTP Request
Example: GET /users/123
🔹 2. DispatcherServlet (Front Controller)
-
Configured by Spring Boot auto-config.
-
Intercepts all incoming requests (
/
mapping). -
Delegates request to appropriate components.
🔹 3. HandlerMapping
-
Finds appropriate
@Controller
class & method based on URL and HTTP method. -
Ex:
@GetMapping("/users/{id}")
🔹 4. HandlerAdapter
-
Executes the controller method.
🔹 5. Controller Method
-
Calls the
Service
layer. -
Business logic executes.
🔹 6. Service Layer
-
Contains business logic
-
Calls the
Repository
(DAO)
🔹 7. Repository Layer
-
Interacts with the DB using JPA/Hibernate.
🔹 8. Return Response
-
Controller returns
ResponseEntity
or object. -
If annotated with
@RestController
or@ResponseBody
, data is serialized (JSON/XML) by HttpMessageConverters (like Jackson).
🔹 9. ViewResolver (If using Thymeleaf/JSP)
-
If returning a view name,
ViewResolver
resolves it to an actual.html
or.jsp
.
🔹 10. Response Sent to Client
✅ Internal Objects in Spring MVC
Component | Purpose |
---|---|
DispatcherServlet | Central controller of Spring MVC |
HandlerMapping | Finds which controller to call |
HandlerAdapter | Calls the actual controller method |
HttpMessageConverter | Converts object to JSON/XML |
ViewResolver | Resolves view names to templates |
ModelAndView | Holds model + view data |
WebDataBinder | Binds request params to Java objects |
🔚 Summary Diagram