Spring Boot Source Code Deep Dive (Part 7): Full Recap and Ultimate Interview Guide
The final installment of the Spring Boot source‑code series consolidates the previous six episodes—covering startup flow, auto‑configuration, IoC container, bean lifecycle, circular dependencies, and SPI—while providing a complete knowledge map, 20 interview‑question templates, practical study tips, and a roadmap for further learning and contribution.
Spring Boot Startup Process
Spring Boot starts in three phases: Preparation phase : infer application type (SERVLET/REACTIVE/NONE), load spring.factories via SpringFactoriesLoader , create Environment (load configuration files), print banner. Container creation phase : create the IoC container according to the inferred type and prepare basic infrastructure (set environment, add BeanPostProcessor ). Refresh phase : invoke refresh() (12 steps). The most critical steps are invokeBeanFactoryPostProcessors (parse bean definitions) and finishBeanFactoryInitialization (instantiate singleton beans). For a web application, onRefresh starts the embedded Tomcat.
@SpringBootApplication Composition
@SpringBootConfiguration (meta‑annotated with @Configuration ) @EnableAutoConfiguration (enables auto‑configuration) @ComponentScan (scans the current package and sub‑packages)
Auto‑Configuration Mechanism
@EnableAutoConfiguration imports AutoConfigurationImportSelector . AutoConfigurationImportSelector uses SpringFactoriesLoader to read all EnableAutoConfiguration entries from META-INF/spring.factories . Conditional annotations ( @Conditional variants) filter which configuration classes become active. Each selected @Configuration class registers beans via @Bean methods.
Circular Dependency Resolution
Spring resolves singleton circular dependencies using a three‑level cache: Level 1 : fully initialized bean (product). Level 2 : early reference (half‑product) exposed to dependent beans. Level 3 : ObjectFactory used to create proxy objects. The core idea is to expose the half‑product early: when bean A depends on B and B on A, A is instantiated, its ObjectFactory is placed in the third‑level cache, B receives the half‑product from the cache, and after B is fully created the reference in A is completed.
Bean Lifecycle
Instantiation → Property population → Aware interfaces → BeanPostProcessor (pre‑initialization) → @PostConstruct → InitializingBean → init‑method → BeanPostProcessor (post‑initialization, AOP proxy) → use → @PreDestroy → DisposableBean → destroy‑method .
BeanFactory vs ApplicationContext
Function : BeanFactory is a basic IoC container; ApplicationContext is an enhanced version supporting internationalization, events, etc. Bean loading : BeanFactory loads beans lazily on getBean ; ApplicationContext eagerly instantiates singleton beans at startup. Event mechanism : Not supported by BeanFactory; supported by ApplicationContext. Internationalization : Not supported by BeanFactory; supported by ApplicationContext. Typical scenario : BeanFactory for resource‑constrained environments; ApplicationContext for general applications.
@Component vs @Bean
Usage location : @Component is placed on classes; @Bean is placed on @Configuration methods. Control : Instances created by the framework for @Component ; developer manually creates the instance in a @Bean method. Applicable scenario : @Component for own classes; @Bean for third‑party classes or beans requiring custom initialization.
Conditional Annotations
Available variants include @ConditionalOnClass , @ConditionalOnMissingClass , @ConditionalOnBean , @ConditionalOnMissingBean , @ConditionalOnProperty , @ConditionalOnWebApplication , @ConditionalOnExpression , etc.
@Import Usage
Directly import a configuration class: @Import(XXXConfig.class) . Implement ImportSelector to return class names. Implement ImportBeanDefinitionRegistrar to register bean definitions programmatically. Use DeferredImportSelector for lazy import (used by auto‑configuration).
BeanFactoryPostProcessor vs BeanPostProcessor
Execution time : BeanFactoryPostProcessor runs after bean definitions are loaded but before any bean is instantiated; BeanPostProcessor runs after bean instantiation, before and after initialization (enables AOP proxying). Target object : BeanFactoryPostProcessor works on BeanDefinition ; BeanPostProcessor works on the actual bean instance. Typical use : BeanFactoryPostProcessor for placeholder resolution, property source processing; BeanPostProcessor for dependency injection, AOP proxy creation.
Spring Event Mechanism
ApplicationEvent : defines an event. ApplicationListener : listens for events. ApplicationEventPublisher : publishes events. By default events are synchronous; the ApplicationEventMulticaster can be replaced with an asynchronous implementation.
@Transactional Working Principle
A proxy object intercepts the target method call. The proxy checks whether the method is annotated with @Transactional . If present, a transaction is started, the target method executes. On exception, if rollback conditions are met the transaction rolls back; otherwise it commits. Note : Internal method calls within the same class bypass the proxy, so the transaction does not take effect.
Running Code Immediately After Spring Boot Starts
CommandLineRunner : receives raw String[] arguments. ApplicationRunner : receives an ApplicationArguments object. @PostConstruct : runs after bean initialization.
Exception Handling in Spring Boot
Local handling with @ExceptionHandler inside a controller. Global handling with @ControllerAdvice + @ExceptionHandler . Custom handling by implementing ErrorController or extending BasicErrorController .
Supported Web Containers
Tomcat is the default. It can be switched to Jetty or Undertow by excluding the Tomcat starter and adding the corresponding dependency: <code><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion>org.springframework.boot:spring-boot-starter-tomcat</exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency></code>
Exclude Attribute in @SpringBootApplication
The exclude attribute allows specific auto‑configuration classes to be omitted, e.g. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) .
Multi‑Environment Configuration
File naming: application-dev.yml , application-prod.yml , etc. Activation methods: spring.profiles.active=dev , command‑line --spring.profiles.active=prod , environment variable SPRING_PROFILES_ACTIVE .
Source‑Code Study Tips
Key entry points: <code>Entry 1: SpringApplication.run() – the heart of Spring Boot startup. Entry 2: @EnableAutoConfiguration – gateway to auto‑configuration. Entry 3: refresh() – core Spring refresh logic. Entry 4: getBean() – bean creation flow.</code> How to avoid getting lost: Combine debugging: set breakpoints and step through the code. Draw diagrams: visualize call chains for quick review. Grab the backbone: read main methods first, then dive into branches. Ask yourself: why does this line exist? What problem does it solve? Version comparison: contrast differences among Spring Boot 2.0, 2.7, 3.0.
Knowledge Map (ASCII Diagram)
┌─────────────────────────────────────────────────────────────────────────────┐
│ Spring Boot Source Code Overview │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ SpringApplication.run() │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Stage 1: Preparation │ │ │
│ │ │ • Infer application type (SERVLET/REACTIVE/NONE) │ │ │
│ │ │ • Load SpringFactoriesLoader (SPI) │ │ │
│ │ │ • Create Environment (load config files) │ │ │
│ │ │ • Print Banner │ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Stage 2: Container Creation │ │ │
│ │ │ • Create IoC container based on application type │ │ │
│ │ │ • Prepare container (set Environment, add BeanPostProcessor)│ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌─────────────────────────────────────────────────────────────┐ │ │
│ │ │ Stage 3: Refresh (refresh() 12 steps) │ │ │
│ │ │ 1. prepareRefresh │ │ │
│ │ │ 2. obtainFreshBeanFactory │ │ │
│ │ │ 3. prepareBeanFactory │ │ │
│ │ │ 4. postProcessBeanFactory (sub‑class hook) │ │ │
│ │ │ 5. invokeBeanFactoryPostProcessors (parse @Bean definitions) │ │ │
│ │ │ 6. registerBeanPostProcessors │ │ │
│ │ │ 7. initMessageSource (i18n) │ │ │
│ │ │ 8. initApplicationEventMulticaster (event broadcaster) │ │ │
│ │ │ 9. onRefresh (start embedded Tomcat for web apps) │ │ │
│ │ │10. registerListeners │ │ │
│ │ │11. finishBeanFactoryInitialization (instantiate singletons) │ │ │
│ │ │12. finishRefresh (publish refresh‑complete event) │ │ │
│ │ └─────────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌───────────────────────────────┐ ┌───────────────────────────────────┐ │
│ │ Bean Lifecycle │ │ Circular Dependency │ │
│ │ Instantiation → Property Fill → Aware → @PostConstruct → … │ │
│ │ → BeanPostProcessor (pre) → init‑method → BeanPostProcessor (post) │ │
│ │ → use → @PreDestroy → DisposableBean → destroy‑method │ │
│ │ → @ComponentScan (current package) │ │
│ └───────────────────────────────┘ └───────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
