Spring Boot 框架的关键组件和内部结构

在我之前的帖子中(介绍 Spring Boot )( / 社区 / 教程 / Spring-boot - 教程),我们已经讨论了 Spring Boot 的基本知识,现在我们将讨论 Spring Boot 的主要组成部分是什么和` Spring Boot 是如何工作的。

春天启动框架的关键组成部分

Spring Boot Framework主要有四个主要组件。

  • 春季启动器
  • 春季启动器自动配置器
  • 春季启动器 CLI
  • 春季启动器执行器

**注:除了这四个主要组件外,还有另外两个 Spring Boot 组件:

  • 春季启动ID
  • 春季启动ID

To quick start new Spring Boot projects, we can use "Spring Initializr" web interface. Spring Initializr URL: https://start.spring.io. We have many Spring Boot IDEs like Eclipse IDE, IntelliJ IDEA, Spring STS Suite etc. We will discuss these two components in coming posts. SpringBootComponents Now we will discuss these Spring Boot four components one by one in detail.

春天游艇开机

Spring Boot Starters 是 Spring Boot Framework 的主要功能或组件之一。 Spring Boot Starter 的主要职责是将一组常见或相关的依赖组合成单个依赖组合。我们将用一个例子详细探讨这个陈述。例如,我们希望使用 Tomcat WebServer 开发一个 Spring WebApplication。然后,我们需要在 Maven 的 pom.xml 文件或 Gradle 的 build.gradle 文件中添加以下最小的 jar 依赖。

  • 春季核心 Jar 文件(春季核心-xx.jar)
  • 春季 Web Jar 文件(春季 Web-xx.jar)
  • 春季 Web MVC Jar 文件(春季 Webmvc-xx.jar)
  • 服务 Jar 文件(服务器-xx.jar)

如果我们想添加一些数据库的东西,那么我们需要添加数据库相关的罐头,如春季JDBC罐头文件,春季ORM罐头文件,春季交易罐头文件等。

  • 春季 JDBC Jar 文件(春季-jdbc-xx.jar)
  • 春季 ORM Jar 文件(春季-orm-xx.jar)
  • 春季交易 Jar 文件(春季-transaction-xx.jar)

We need to define lot of dependencies in our build files. It is very tedious and cumbersome tasks for a Developer. And also it increases our build file size. What is the solution to avoid this much dependencies definitions in our build files? The solution is Spring Boot Starter component. Spring Boot Starter component combines all related jars into single jar file so that we can add only jar file dependency to our build files. Instead of adding above 4 jars files to our build file, we need to add one and only one jar file: "spring-boot-starter-web" jar file. When we add "spring-boot-starter-web" jar file dependency to our build file, then Spring Boot Framework will automatically download all required jars and add to our project classpath. spring-boot-starter-dependencies In the same way, "spring-boot-starter-logging" jar file loads all it's dependency jars like "jcl-over-slf4j, jul-to-slf4j,log4j-over-slf4j, logback-classic" to our project classpath.

春天起飞机的主要优点

  • Spring Boot Starter 可减少定义许多依赖性,简化项目构建依赖性.
  • Spring Boot Starter 可简化项目构建依赖性。

这是关于Spring Boot Starter的组件,我们将在即将到来的帖子中讨论一些更详细的Spring Boot示例。

春天启动自动配置

Another important key component of Spring Boot Framework is Spring Boot AutoConfigurator. Most of the Spring IO Platform (Spring Framework) Critics opinion is that "To develop a Spring-based application requires lot of configuration (Either XML Configuration of Annotation Configuration). Then how to solve this problem. The solution to this problem is Spring Boot AutoConfigurator. The main responsibility of Spring Boot AutoConfigurator is to reduce the Spring Configuration. If we develop Spring applications in Spring Boot,then We dont need to define single XML configuration and almost no or minimal Annotation configuration. Spring Boot AutoConfigurator component will take care of providing those information. For instance, if we want to declare a Spring MVC application using Spring IO Platform, then we need to define lot of XML Configuration like views, view resolvers etc. But if we use Spring Boot Framework, then we dont need to define those XML Configuration. Spring Boot AutoConfigurator will take of this. If we use "spring-boot-starter-web" jar file in our project build file, then Spring Boot AutoConfigurator will resolve views, view resolvers etc. automatically. And also Spring Boot reduces defining of Annotation configuration. If we use @SpringBootApplication annotation at class level, then Spring Boot AutoConfigurator will automatically add all required annotations to Java Class ByteCode. SpringBootApplicationAnnotation If we go through Spring Boot Documentation, we can find the following definition for @SpringBootApplication.

1@Target(value=TYPE)
2@Retention(value=RUNTIME)
3@Documented
4@Inherited
5@Configuration
6@EnableAutoConfiguration
7@ComponentScan
8public @interface SpringBootApplication

也就是说, @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration. 这是关于Spring Boot AutoConfigurate组件. 我们将在即将到来的帖子中讨论一些Spring Boot示例的更多细节。

*简而言之,Spring Boot Starter 减少了构建的依赖性,而Spring Boot AutoConfigurator 减少了Spring Boot AutoConfiguration.

  • 正如我们所讨论的,Spring Boot Starter 对Spring Boot AutoConfigurator 有依赖性,Spring Boot Starter 会自动触发Spring Boot AutoConfigurator

春天CLI

Spring Boot CLI(命令线接口)是一个 Spring Boot 软件,用于运行和测试 Spring Boot 应用程序从命令提示。当我们使用 CLI 运行 Spring Boot 应用程序时,它会内部使用 Spring Boot Starter 和 Spring Boot AutoConfigurate 组件来解决所有依赖并执行应用程序。我们甚至可以使用简单的 Spring Boot CLI 命令运行 Spring Boot Web 应用程序。

1spring run HelloWorld.groovy

在这里,HelloWorld.groovy是一个Groovy脚本FileName。类似于Java源文件名具有 *.java扩展,Groovy脚本文件具有 *.groovy扩展。春天命令执行HelloWorld.groovy并产生输出。春天启动CLI组件需要许多步骤,如CLI安装,CLI设置,开发简单的春天启动应用程序并测试它。所以我们将专注于另一个帖子以详细地讨论它与一些春天启动例子。

春天启动器

Spring Boot Actuator 组件提供了许多功能,但两个主要功能是

  • 向 Spring Boot 应用程序提供管理终端点
  • Spring Boot 应用程序指标

當我們使用 CLI 執行 Spring Boot Web 應用程式時,Spring Boot Actuator 會自動提供主機名稱為 "localhost" 和默認端口號為 "8080". 我們可以使用 "https://localhost:8080/" 終點存取此應用程式。 我們實際上使用 HTTP 要求方法,如 GET 和 POST,以代表使用 Spring Boot Actuator 的管理終點。 我們將在未來的帖子中討論更多關於 Spring Boot Actuator 的細節。

春季船舶框架的内部

它总是建议了解 Spring Boot Framework 如何减少 build 的依赖性,Spring Configuration,等等. Spring Boot 是如何在这个过程中运作的。如果你熟悉 Groovy 编程语言,那么你知道大多数事情。在 Groovy,我们不需要添加一些导入,也不需要向 Groovy 项目添加一些依赖性。当我们使用 Groovy Compiler(groovyc)编译 Groovy 脚本时,它会自动添加所有默认导入陈述,然后编译它。同样, Groovy 编程语言包含一个 JAR 依赖性解决器来解决并将所有所需的 jar 文件添加到 Groovy 项目类路径中。 Spring Boot Framework 内部使用 Groovy 添加一些默认内容,如默认导入陈述,主应用() 方法等。

葡萄

Grape is an Embedded Dependency Resolution engine. Grape is a JAR Dependency Manager embedded into Groovy. Grape lets us quickly add maven repository dependencies to our project classpath to reduce build file definitions. Spring Boot Framework programming model is mainly inspired by Groovy Programming model. Spring Boot Framework internally depends on these two major components: Groovy and Grape. springboot-internals You can refer Grape documentation https://docs.groovy-lang.org/latest/html/documentation/grape.html for more details. That's it about Spring Components and Internals. We will discuss about these components in details with some Spring Boot examples in coming posts. If you have any queries, please drop me a comment.

Published At
Categories with 技术
Tagged with
comments powered by Disqus