What is springboot
It is a javaweb development framework, similar to SpringMVC. Compared with the benefits of other javaweb frameworks, it is officially said to simplify development. The convention is greater than the configuration you can "just run", which can quickly develop web applications and develop an http interface with a few lines of code.
Spring Boot is developed based on Spring. Spirng Boot itself does not provide the core features and extended functions of the Spring framework. It is only used to quickly and agilely develop a new generation of applications based on the Spring framework. In other words, it is not a solution to replace Spring, but a tool that is closely integrated with the Spring framework to enhance the Spring developer experience. With the core idea of convention greater than configuration, SpringBoot helps us with a lot of settings by default. Most Spring Boot applications require very little Spring configuration. At the same time, it integrates a large number of commonly used third-party library configurations (such as Redis, MongoDB, Jpa, RabbitMQ, Quartz, etc.). These third-party libraries in Spring Boot applications can be used out of the box with almost zero configuration. In short, SpringBoot is actually It is not a new framework. It is configured with many frameworks by default, just like maven integrates all jar packages and spring boot integrates all frameworks.
Spring Boot was born and stood at a relatively high starting point. After several years of development, the ecology is sufficiently perfect. Spring Boot has become the hottest technology in the Java field.
Advantages of springboot
- Get started faster for all Spring developers
- Out of the box, various default configurations are provided to simplify project configuration
- Embedded container simplifies web projects
- No redundant code generation and XML configuration requirements
The first springboot program
- Create a project with idea
Such a springboot project is created.
You can see this main function, run it and then access the local port 8080. The result is as shown in the figure below: It
means that it is half the success.
- Then we create a controller package under the created project name package, and write the following program:
package com.lei.helloworld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello springboot";
}
}
Then visit hello, and the results are as follows: It
can be seen that this is very similar to the springmvc that we learned before, but it is much easier than springmvc. From creating an empty project to now, we have written the controller, and we can directly access it, saving a lot of Configuration file, it can be seen that springboot is automatically assembled.
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lei</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
It can be seen that all the dependencies imported by springboot start with spring-boot-starter.
-
Some people may also encounter the failure of the main function to run because port 8080 is occupied, so this problem can also be easily solved here:
write the above program in the corresponding file, restart the project, and you can realize the port replacement. -
Change the pattern at the beginning of the main function runtime:
create banner.txt in the following directory, then put the pattern you like into it, restart the project, and it will be successful.
A preliminary exploration of the principle
Automatic configuration
pom.xml
- spring-boot-dependencies: The core dependencies are in the parent project!
- When we write or introduce some springboot dependencies, we don’t need to specify the version because there are these version repositories.
Launcher
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- Starter: To put it bluntly, it is the startup scene of Springboot;
- For example, spring-boot-starter-web, he will help us automatically import all the dependencies of the web environment!
- springboot will turn all the functional scenarios into one starter
- What function we want to use, we only need to find the corresponding starter.
Main program
@SpringBootApplication //标注这个类是一个springboot应用
public class HelloworldApplication {
public static void main(String[] args) {
//将springboot应用启动
SpringApplication.run(HelloworldApplication.class, args);
}
}
All automatic configuration of springboot is scanned and loaded at startup: all the automatic configuration classes of spring.factories are in it, but they may not take effect. It is necessary to judge whether the condition is established, as long as the corresponding start is imported, there will be a corresponding start With the launcher, our automatic assembly will take effect, and then the configuration will be successful!
This is the principle of automatic assembly! The essence:
1) SpringBoot will load a large number of automatic configuration classes
2) Let's see if the functions we need are in the automatic configuration classes written by SpringBoot by default;
3) Let's look at this automatic configuration class Which components are configured in the middle; (As long as the components we want to use exist in them, we don’t need to configure them manually)
4). When adding components to the automatic configuration class in the container, certain properties are obtained from the properties class. We only need to specify the values of these attributes in the configuration file;
xxxxAutoConfigurartion: automatic configuration class; add components to the container
xxxxProperties: package related properties in the configuration file;