1, What is SpringBoot?
Spring Boot is a new framework provided by pivot team. It is designed to simplify the initial construction and development process of new spring applications. The framework uses a specific way to configure, so that developers no longer need to define templated configurations.
2, Characteristics
- Create a stand-alone Spring application
- Embedded Tomcat, no need to deploy WAR file
- Simplify Maven configuration
- Auto configure Spring
- Provide production ready functions such as indicators, health checks and external configurations
- There is absolutely no code generation and no configuration requirements for XML
3, Write Spring Boot's Hello World
1. Environment for installing Spring Boot
Create a Maven project (I use IDEA2019.3.x), or directly create a Spring Initializr project. You can directly write a helloworld file. You can view:
SpringBoot is created directly without configuring the pom.xml path
Install the SpringBoot environment using the pom.xml file
pom.xml file:
<packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Previously, when learning spring MVC, our packaging method was
<packaging>war</packaging>
When writing a SpringBoot project, you can change it to jar
<packaging>jar</packaging>
In this way, it is convenient to package and send to others after the completion of later projects, so there is no need to configure this environment.
After the environment required in the pom.xml package is written, wait for the download. After the download, the environment configuration is completed.
2. You are now ready to create a java file to write
SpringBoot is a further simplification of other Spring design patterns. There is no need to configure mapper.xml or a separate tomcat. Just use one controller to execute.
The properties file in the resources is used to configure the port number of tomcat and some other variable quantities.
In the HelloController.java file, it is the same as the previous spring MVC. Use @ RequestMapping to set a path for the Hello method
HelloController.java
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"; } }
MySpringBoot.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Main program class * @SpringBootApplication:This is a SpringBoot application */ @SpringBootApplication public class MySpringBootTest { public static void main(String[] args) { SpringApplication.run(MySpringBootTest.class,args); } }
3. Run
Run the MySpringBootTest file directly
This indicates that the project was created successfully.
You can view it in the browser
Done.