[source code analysis] why? spring boot a jar can develop a web project

Keywords: Java Spring Tomcat Hibernate Apache

problem

Why to develop a web project, spring boot starter web is done with a jar? What does this jar do?

Through the spring boot project, we can see that all the boot modules spring boot starter XXX that are used out of the box are in the spring boot starters sub module, and all the spring boot starter XXX modules have no code, so they all complete the corresponding functions in other packages. First, analyze its dependence

rely on

Note: the Jakarta.xxxx package in the figure is the original javax.xxxx package. The Java EE has been renamed Jakarta EE. The spring boot-starter-web-2.1.8.release version is directly dependent on the hibernate validator. The spring boot-2.2.0 version started to use Jakarta, and a new module spring boot starter validation is used to manage it.

As can be seen from the dependency graph, the most core spring boot depends on spring context and spring core. Therefore, as the official said, spring boot is based on spring.

Spring boot makes it easy to create a runnable, stand-alone, production level spring based application. We have our own views on the spring platform and third-party libraries so you can start with the least hassle. Most spring boot applications require very little spring configuration.
You can use Spring Boot to create Java applications, which can be deployed using java jar or more traditional war. We also provide a command-line tool to run the spring script.

Our primary objectives are:

  • Provide a faster, easier to get started experience for all spring development.
  • Out of the box, but change quickly when requirements start to deviate from the default.
  • Provides a range of non functional features common to large projects (such as embedded servers, security, traffic, health checks, and externalized configurations).
  • Absolutely no code generation or XML configuration is required.

Spring boot dependency management

[spring boot source code analysis] spring boot dependency management diagram

Spring boot starter (important)

This module is the core of all spring boot starter XXXX bootstrappers, very important!

It includes the following modules:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
</dependency>
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <scope>runtime</scope>
</dependency>

spring-boot

Spring boot kernel and spring boot feature functions are implemented in this package.

spring-boot-autoconfigure

Spring boot automatic configuration, which provides the default configuration of some common packages

[source code analysis] I don't know the details of automatic configuration, let alone you can spring boot.

spring-boot-starter-logging

Spring boot default log booter

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
</dependency>

Without doing anything, several dependencies are introduced. What problems are solved by introducing these dependencies?
Through the introduction of these dependencies, jar required by slf4j and logback log framework, as well as the adaptation of log4j and jul log tools to slf4j are introduced directly or indirectly.
Therefore, this jar is introduced, and the log implementation in the project uses logback.
Uh huh? What about log4j2?
Originally, if you want to use log4j2, there is a spring boot starter log4j2 package for us to choose from.

For the specific configuration of logs, it is recommended to use the original xml configuration instead of yaml or properties.
For example: logback is configured with logback-spring.xml under the root directory.

snakeyaml

A toolkit for generating and parsing yaml syntax

SnakeYaml quick start

spring-boot-starter-tomcat

Without any processing, use the embedded Tomcat related jar directly. Spring project calls spring framework from external tomcat, while spring boot calls embedded Tomcat from internal framework. The active passive relationship has been transformed.

<!-- Equivalent to removed tomcat-annotations-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-annotations-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-el</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
</dependency>

spring-boot-starter-json

Without any processing, use jackson as the default json tool

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-parameter-names</artifactId>
</dependency>

spring-boot-starter-validation

Version 2.1.8.RELEASE is directly dependent on Hibernate validator and does not have this module.

Version 2.2.0 relies on Jakarta.validation-api and hibernate-validator, and removes javax.validation-api from hibernate-validator.

There is no difference between the two when they are used. They are switched senselessly.

validator automatic verification

<!-- 2.2.0.M6 -->

<!-- Equivalent to removed javax validation-api -->
<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-el</artifactId>
</dependency>

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Reference material

Posted by first_lady_the_queen on Sat, 19 Oct 2019 05:43:20 -0700