Article directory
SpringBoot
SpringBoot starter
The SpringBoot entry procedure steps are as follows :
Create a new module, select Spring initialization, and configure basic information about the module
Select the technology set that the current module needs to use
Create a controller class
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String selectById(@PathVariable Integer id) {
System.out.println("id ===> " + id);
return "hello spring boot";
}
}
Run the automatically generated Application class
At this point, our SpringBoot entry program is finished, and we can use PostMan to send requests for testing
We can find that the SpringBoot program is much simpler than the Spring program development
class/config file | Spring | SpringBoot |
---|---|---|
Coordinates in the pom file | Add manually | Tick to add |
web3.0 configuration class | Handmade | none |
Spring/SpringMVC configuration class | Handmade | none |
controller | Handmade | Handmade |
Note: Developing SpringBoot programs based on idea needs to ensure networking and be able to load into the program framework structure
SpringBootQuick start
The SpringBoot project can be separated from IDEA and run independently without relying on Tomcat and Maven
For example, in development, the front-end personnel want to do some debugging. In addition to allowing the front-end personnel to connect to the server on our computer, we can also package the SpringBoot project and send it to the front-end personnel. Just make sure that the connection is the same database.
The quick start steps are as follows :
Package the SpringBoot project (execute the Maven build instruction package)
Execute startup naming in the console: Start the jar package packaged by SpringBoot
java –jar 文件名称.jar
We package the project into a jar package, and the main contributor to running the project by running the jar package is the plug-in in the pom.xml file
This plug-in not only packs the project into a jar package, but also packs other jar packages needed by the project into the final generated jar package; and sets the entry program;
jar supports command line startup and needs to rely on maven plug-in support, please confirm whether there is a maven plug-in corresponding to SpringBoot when packaging
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Basic introduction to SpringBoot
SpringBoot is a new framework provided by the Pivotal team, which is designed to simplify the initial construction and development process of Spring applications
Spring program disadvantages
- Configuration is cumbersome
- Dependency settings are cumbersome
Advantages of SpringBoot program
- automatic configuration
- Starter dependencies (simplified dependency configuration)
- Accessibility (built-in server, ...)
How to start the SpringBoot program :
When SpringBoot creates a project, it uses the jar packaging method
The boot class of SpringBoot is the entry point of the project, and the project can be started by running the main method
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
SpringBoot starting dependency
starter
- The common project name in SpringBoot defines all project coordinates used by the current project to achieve the purpose of reducing dependency configuration
parent
- All SpringBoot projects
都要继承的项目
define several coordinate version numbers (dependency management, not dependency) to reduce dependency conflictsThere are 57 different coordinate versions between spring-boot-starter-parent (2.5.0) and spring-boot-starter-parent (2.4.6)
actual development
When using arbitrary coordinates, only write G and A in GAV, and V is provided by SpringBoot
If a coordinate error occurs, specify the version again (be careful of version conflicts)
SpringBoot switch server
We currently use the Tomcat server in our SpringBoot project. For example, how do we configure the Jetty server if we want to use it?
First, you need to exclude the dependency of the Tomcat server
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--Web起步依赖中, 排除Tomcat依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Add jetty server dependency
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--Web起步依赖中, 排除Tomcat依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加jetty起步依赖, 版本由SpringBoot的starter控制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Jetty is more lightweight and scalable than Tomcat (compared to Tomcat), Google Application Engine (GAE) has been fully switched to Jetty
Tomcat has some things built in, whether you use it or not
There is nothing built into Jetty, what needs to be added by yourself
If you use many things, choose Tomcat, if you use few things, choose Jetty
We can find it very convenient to use SpringBoot to develop and switch dependencies