Why should you learn Spring Boot?
Spring was born to simplify the development of Java programs, and Spring Boot was born to simplify the development of Spring programs.
Spring Boot is the scaffolding of the Spring framework. It was born for the rapid development of the Spring framework.
Quickly integrates frameworks. Spring Boot provides the function of starting to add dependencies, which is used to integrate various frameworks in seconds.
Built-in running container, no need to configure Web containers such as Tomcat, run and deploy programs directly.
Rapidly deploy projects to get your projects up and running without the need for external containers.
You can completely abandon the cumbersome XML and use annotations and configuration for development.
Supports more monitoring indicators to better understand the running status of the project.
Create using Idea [provided by ide developer]
Web version creation method [Officially provided by Spring]
The Spring Boot project has two main directories:
src/main/java is the Java source code.
src/main/resources is static resources or configuration files:
/static: static resource folder;
/templates: template resource folder.
Click the main method of the startup class to run the Spring Boot project. The successful startup is as shown in the following figure:
We learn JavaEE to implement Web projects or interfaces. Before, Spring was actually an ordinary Java project, and there was no way to interact directly with the browser, so next we have to use Spring Boot is used to interact with browsers and users.
Create the HelloController file under the created project package path. The implementation code is as follows:
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/hi")//路由映射 public class HelloController { @RequestMapping("/index")//路由映射 @ResponseBody//返回一个非静态页面的数据 public String sayHi(){ return "你好,Spring Boot"; } }
Restart the project and visit http://localhost:8080/hi/index. The final effect is as follows:
We try to move HelloController to other packages, such as the following methods:
Run our project and found that the program reported an error, as shown in the following figure:
This means that the Spring Boot project did not inject objects into the container middle.
When we put the container class and startup class to be injected into the same directory, as shown in the following figure:
At this time, the Spring Boot project can normally inject beans into the container.
Convention is greater than configuration
The above situation reflects another feature of the Spring Boot project: convention is greater than configuration.
We can also see this feature in Spring projects. For example, in Spring, the scanning path of beans must be configured, but in Spring Boot, this is not required. The Spring configuration is as follows:
Note:
The classes annotated in the 5 major categories must be placed in the same directory as the startup class or in a subdirectory of the startup class, otherwise they will not be recognized
The above is the detailed content of What are the basic operations and concepts of getting started with SpringBoot?. For more information, please follow other related articles on the PHP Chinese website!