Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SpringBoot] SpringBoot是如何启动的 #185

Closed
zhonghuasheng opened this issue Mar 5, 2020 · 1 comment
Closed

[SpringBoot] SpringBoot是如何启动的 #185

zhonghuasheng opened this issue Mar 5, 2020 · 1 comment
Assignees
Milestone

Comments

@zhonghuasheng
Copy link
Owner

No description provided.

@zhonghuasheng zhonghuasheng added this to the Step#1 milestone Mar 5, 2020
@zhonghuasheng zhonghuasheng self-assigned this Mar 5, 2020
@zhonghuasheng
Copy link
Owner Author

Spring Boot运行原理

在前面的篇幅中我们重点阐述了为什么Spring Boot可以实现高度地自动化配置(组合注解,条件注解)。那么,接下来我们就结合Spring Boot最核心的组合注解@SpringBootApplication来分析下Spring Boot的项目到底是怎么启动运行的。

@SpringBootApplication注解实际上是一个组合注解,除了对应用开放的@componentscan注解(实现对开发者自定义的应用包扫描)外,其最核心的注解就是注解@EnableAutoConfiguration,该注解表示开启自动配置功能,而在具体的实现上则是通过导入注解@import(EnableAutoConfigurationImportSelector.class)类的实例,在逻辑上实现了对所依赖的核心jar下META-INF/spring.factories文件的扫描,该文件则声明了有哪些自动配置需要被Spring容器加载,从而Spring Boot应用程序就能自动加载Spring核心容器配置,以及其他依赖的项目组件配置,从而最终完成应用的自动初始化,通过这种方法就向开发者屏蔽了启动加载的过程。

如“spring-boot-autoconfigure”核心包中的META-INF/spring.factories文件就是定义了需要加载的Spring Boot项目所依赖的基础配置类,如Spring的容器初始化配置类等。如:

# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer
.....
而对于大部分第三方需要与Spring Boot集成的框架,或者我们日常开发中需要进行抽象的公共组件而言,得益于这种机制,也可以很容易地定制成开箱即用的各种Starter组件。而使用这些组件的用户,往往只需要将依赖引入就好,不再需要进行任何额外的配置了!

Spring Boot后记
以上就是Spring Boot运行的基本原理了,希望这篇文章能够对你有所帮助!实际上学习Spring Boot进行项目开发关键就是要掌握各种Spring及Spring Boot的各种注解,特别是一些关键核心注解。同样在进行基于Spring Cloud微服务的开发中,也是需要理解Spring Cloud相关组件所提供的各种核心注解,只有这样才能更好的理解框架的原理及使用,而不只是云里雾里地进行各种似懂非懂的Copy开发。

原文链接:https://blog.csdn.net/cowbin2012/article/details/88861160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

1 participant