Why do we need SpringBoot when we have Spring?

Table of contents

1. Spring shortcomings analysis

2. What is Spring Boot?

3. Core functions of Spring Boot

3.1 Starting dependencies

3.2 Automatic assembly


1. Spring shortcomings analysis

1. Too many configuration files and dependencies! ! !

Spring is a very excellent lightweight framework, with IOC (Inversion of Control) and AOP (Aspect Oriented) as its ideological core, which greatly simplifies the development of JAVA enterprise-level projects. Although Spring's component code is lightweight, its configuration is heavyweight. Using Spring for project development requires writing a lot of code in the configuration file, and all of these configurations represent losses during development.

For example, the picture below reflects how cumbersome the configuration file is when configuring data sources! ! !

In addition, dependency management of Spring projects is also a time-consuming and labor-intensive matter. When setting up the environment, it is necessary to analyze the coordinates of which libraries are to be imported, and also the coordinates of other libraries that are dependent on it. Once the wrong dependent version is selected, the ensuing incompatibility problems will seriously hinder The development progress of the project. For example, Spring 5.0 and above can only use Junit 4.12 and above. 

The following figure well explains the horror of adding dependencies to the pom file developed by the spring framework:

Summarize
the shortcomings of Spring:

  • Configuration is too cumbersome.
  • Too many dependencies are introduced and version control is complicated

2. What is Spring Boot?

SpringBoot improves and optimizes the shortcomings of Spring. Based on the idea that convention is greater than configuration, it simplifies the development of Spring. The so-called simplification means that it simplifies the large number of configuration files and cumbersome dependency introduction in Spring. So SpringBoot is a framework that serves the framework. It is not an enhancement to Spring's functions, but provides a way to quickly use the Spring framework.

Advantages of SpringBoot:

  • Simple configuration
  • Dependencies are easy to introduce
  • Provides some non-functional features for large projects, such as embedded servers, security indicators, health monitoring, etc. 

3. Core functions of Spring Boot

The core function of Spring Boot is starting dependencies and automatic assembly

3.1 Starting dependencies

SpringBoot's dependencies are based on functions, rather than the dependencies of ordinary projects on JAR packages. SpringBoot packages all the coordinates needed to complete a function and completes version adaptation. We only need to introduce a dependency when using a certain function.

The principle is Maven's transitive dependency. For example, a depends on b, b depends on c, and c depends on d. Then if we introduce a dependency, the remaining dependencies of b, c, and d will be loaded.

In Spring Boot, it mainly introduces parent dependencies. Let’s click in to see what the parent dependencies introduce.

We can see that a parent dependency continues to be introduced. Let's ctrl and click again to see 

Ok, we can see that many dependent versions are declared here, so this is why Spring Boot does not need to declare versions when introducing dependencies. They have all been defined in the parent dependencies.

3.2 Automatic assembly

     The SpringBoot project automatically provides optimal configuration, and the default values ​​can be modified to meet specific requirements.

1. View the source code of the annotation @SpringBootApplication

@SpringBootConfiguration is equivalent to @Configuration, which means that this is a SpringBoot configuration class, which is called Configuration in spring.

@Enable Auto Configuration means turning on the automatic configuration function.

2. Let’s +ctrl click @Enable Auto Configuration to see what’s inside

3. From here we can only see that the @Import annotation imports the AutoConfigurationImportSelector class. Now let’s +ctrl to see what is written in AutoConfigurationImportSelector.

The main thing is the getCadicateConfigurations method above, which calls the SpringLoaderFactories.loadFactoryNames method. The return value from the call is a configurations collection. You can guess that the main function of this method is to obtain all configuration classes. And added a sentence at the end:

No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.

Translation: No autoconfiguration classes found in META-INF/spring.factories. If you are using custom packaging, make sure the file is correct.

This means that this method mainly finds the configuration classes that need to be loaded in META-INF/spring.factories and reads the names of all configuration classes:

Let's see what's in the META-INF/spring.factories file?

The above configuration information means that there are a large number of class names ending with Configuration. The classes here are classes with automatic configuration information. SpringBootApplication obtains the names of these classes before loading them.

Let us take the ServletWebServerFactoryAutoConfiguration class as an example to analyze the source code

There is another automatic loading here: @EnableConfigurationProperties({ServerProperties.class}), which represents the automatic loading of the server configuration class ServerProperties. Let us ctrl click to see it.

prefix = "server", which means that the prefix of the Spring Boot configuration file is server. SpringBoot will automatically map attributes starting with the server prefix to this type of field. For example, when we configure the network port, we set server.port = 8080,

If we do not configure it, Spring Boot will read the default configuration information, and the default configuration information is placed in the spring-configuration-metadata.json file, as shown below:

This file also stores all default configuration information! ! !

Summarize

Through starting dependencies and automatic assembly, a lot of trouble is saved during development. There is no need for version management and various configurations. Everything is done in one step through automatic assembly in Spring Boot.

Guess you like

Origin blog.csdn.net/qq_53317005/article/details/132957942