96SEO 2026-08-02 00:54 0
了解 Spring 框架,就必须弄清楚 Spring 项目启动后到底发生了什么。很多同学在调试时常常卡在以下几个点上:
SpringApplication.run 背后到底执行了哪些步骤。下面以 Spring Boot 为例。层层拆解这些疑惑,让你彻底掌握 IOC 的实现细节。

SpringApplication 实例——它是整个启动流程的总调度指挥。并非容器本身,而是负责统筹初始化到运行的全流程。其实,
SpringApplication.run
主要功能:
SERVLET: Web 项目REACTIVE: 响应式 WebFlux 项目NONE: 普通 Java 项目ApplicationContextInitializer这些初始化器会在容器创建后、refresh 前执行,用来预处理或修改容器配置。ApplicationListener),监听启动过程中的关键节点:starting、environment、context、ready 等。SpringApplicationRunListeners),并发布 ApplicationStartingEvent示例命令:
java -jar app.jar --spring.profiles.active=dev --server.port=8081
- 解析为 DefaultApplicationArguments;参数优先级最高,会覆盖配置文件和环境变量。- 建立统一配置入口 Environment。管理以下
The key event: ApplicationEnvironmentPreparedEvent
ApplicationContextInitializer),对环境做进一步定制。
/\\ / ___'_ __ _ __ __ __ _
\ \ \ \\___ | '_ | '_| | '_ \/ _` |
\ \ \ \ \/ ___)| |_)| | | | | || (_| |
) ) ) ) ' |____| .__|_| |_|_| |_\
==========|_|==============|___/=/_/_/_/
至此,我们已经准备好进入真正的容器建立阶段。
Spring 会检查 Classpath 中是否存在特定类,从而自动判定使用哪种容器:
*Reactive Web 环境* : 存在
`WebFlux `→话说回来,创建
`AnnotationConfigReactiveWebServerApplicationContext `
li>*普通单机应用* : 上述都不存在 →创建
`AnnotationConfigApplicatio nContext `
p>一句话这方面。项目里有什么依赖,Spring 就给你造什么容 器!这正是约定大于配置的体现。
li>
li>
p
ol start = "2"
Oops we need to correct output and finish properly.
SpringApplication.run’ 背后执行的每一步骤。下面以 Spring Boot 为例。一步步拆 解这些疑惑,让你彻底掌握 IOC 的实现细节。
Ol Start=”9” Li> Run all registered ApplicationContextInitializers – y may tweak Environment before beans are loaded. Li> Print banner – that ASCII art you see when you start a Spring‑Boot app.
Pre Code Class=“language-text” Style=”white‑space: pre;background:#fff;说起来,padding: 10Px;border-radius: 6Px;border: 1Px solid #ddd;"> /\ /\ ' _ _ _ _ _ _ _ \ \ \ \ | '' | '| '' \/ _` | \ \ \ \ \/ )| |)| | | | || (| | ) ) ) ) ' || .|| ||| |\ ==========||==============|/=///_/ CoDe>/Pre>
P>The bootstrapping phase ends here – now we move on to real container creation.
OL START=”1″ STYLE=”margin-left: 30PX;"> LI STYLE=”margin-bottom: 8PX;">Create appropriate ApplicationContext based on application type.
UL STYLE=”list‑style‑type:square;老实说,margin-left: 15PX;"> LI STYLE=”margin-bottom: 4PX;">* Servlet web environment *: if classes such as javax.servlet.Servlet or spring‑mvc are present → instantiate .
LI STYLE=”margin-bottom: 4PX;">* Reactive web environment *: if WebFlux is present → instantiate .
LI STYLE=”margin-bottom: 4PX;">* Plain non‑web application *: none of above → instantiate . UL>
OL START=”2″ STYLE=”margin-left: 30PX;"> LI STYLE=”margin-bottom: 8PX; ">Add initial configuration to newly created context.
UL STYLE=”list‑style:none;margin-left: 20PX;"> LI STYLE=”font-size:.95EM;color:#555,margin-bottom:
6PX;">Set prepared Environment into context:
padding:
3px
border-radius:
3
context.setEnvironment;CODE>
6PX;">Create a fresh DefaultListableBeanFactory – this factory actually creates bean instances.
LI STYLE…”font-size:.95EM;color:#555,margin-bottom:
6PX;">Initialize core utilities .
6PX;">Mark context state as created but not refreshed.
UL>
OL START=”3″ STYLE=”margin-left:
30 PX;">
LI STYLE,”font-size:.95EM;color:
bottom这方面,8 PX;">The heavyweight part begins – calling refresh. This method orchestrates every step of bean loading.
UL ST YLE=
‘list-
style-
说到type,disc;不过,margin-
至于left,20 PX;’>
‘font-size
:.95EM;按理说,color
:#777;’
prepareRefresh – lock context to prevent concurrent refreshes,set active profiles。clear cached metadata.
‘font-size
:.95EM;color
:#777,’
obtainFreshBeanFactory – discard old factory if any and create a brand new DefaultListableBeanFactory with proper ClassLoader。BeanExpressionResolver and AutowireCandidateResolver.
‘font-
size的观点是,.95EM;color
:
’
scan components – execute @ComponentScan based on main application’s package,generate BeanDefinition objects and register m in BeanDefinitionRegistry.
’
postProcessBeanFactory – invoke all ConfigurableListableBeanFactoryPostProcessor beans。allowing modification of bean definitions before any bean gets instantiated.
‘hljs LANGUAGE-JA’
STYLE='background:
6Px;border-radius:
8Px;border:
1Px solid #ddd;怎么说呢,'>
void postProcessBeanFactory{
// custom logic can add/remove/modify BeanDefinitions here
}
CODE
’
registerBeanPostProcessors – locate every BeanPostProcessor definition。instantiate m early and add m to factory’s post‑processor chain.
3Px;border-
至于radius,3Px;'>
beanFactory.addBeanPostProcessor;CODE
’
initializeMessageSource – set up internal MessageSource for internationalization.
’
initApplicationEventMulticaster – prepare an EventMulticaster for publishing later events.
’
registerListeners – turn all ApplicationListener beans into listeners of that multicaster.
’
instantiate non‑lazy singleton beans – freeze bean definitions,n create every singleton that isn’t marked lazy by executing its full lifecycle
(instantiation → dependency injection → BeanPostProcessor.beforeInitialization → init methods →
BeanPostProcessor.afterInitialization → registration in singleton cache).
UL END
Li Style =
‘ margin-
top :
10 Px ’>
PUBLISH CONTEXT REFRESHED EVENT:
The whole container is now fully initialized.
Code end.
H h h H H H H h h h hh
LI STYLE=”font-size:.95EM;color:#555,margin-bottom:
LI STYLE…其实,”font-size:.95EM;color:#555,margin-bottom:
LI_STYLE=
LI_STYLE=
LI_STYLE=
LI_STYLE=
CODE CLASS=
LI_STYLE=
CODE CLASS=
LI_STYLE=
LI_STYLE=
LI_STYLE=
LI_STYLE=
="">
Li styl e=' margin–bottom :
8 Px;'> Start embedded Tomcat . The server starts after refresh because it needs beans such as DispatcherServlet,Filter and Servlet which are only created once context is fully ready.
Li Styl e=' font-s iz e :
13 Px;说起来,'>Create Tomcat instance.
13 Px;'>Load connectors and context configuration.
13 Px;说起来,'>Register DispatcherServlet bean.
13 Px;其实,'>Register Filters &,amp;amp,amp;amp,amp;amp,amp;a nd Listeners defined in your code or via auto‑configuration.
e:
13 Px;'>Bind port .
13 Px;'>Start Tomcat’s acceptor thread so it begins listening for HTTP requests. UL>
Li>You call `SpringApplication.run` → it builds a `SpringApplication` object that prepares an `Environment`,loads initializers/listeners and decides which `ApplicationContext` subclass to create.
Li>The chosen `ApplicationContext` creates a fresh `DefaultListableBeanFactory`. Then `refresh` performs a series of well‑defined steps:
• Prepare refresh
• Create fresh bean factory
• Scan packages → register `BeanDefinition`s
• Apply `BeanFactoryPostProcessor`s
• Register all `BeanPostProcessor`s
• Initialize message source &,amp;amp,a nd event multicaster
• Instantiate all non‑lazy singletons
• Publish `ContextRefreshedEvent`.
Li>If it’s a servlet web app,after refresh Spring Boot starts an embedded Tomcat which finally registers DispatcherServlet etc.。and your service becomes reachable via HTTP.
Li>If it’s reactive or non‑web,corresponding contexts take over without starting a servlet container.
Ul>
。作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback