Git Product home page Git Product logo

Comments (1)

zhonghuasheng avatar zhonghuasheng commented on May 21, 2024

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

from tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.