一、SpringBoot入门  1.SpringBoot是什么
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
  Spring Boot可以轻松创建独立的,生产级别的基于Spring 的应用程序,您只需要运行它即可 我们对Spring和其他第三方库采取了自己的观点,因此您可以最低限度的运行Spring程序,大部分的Spring应用程序也只需要很少的配置   SpringBoot使用"约定大于配置"的理念,可以使你的项目快速运行起来   我们可以简单的理解springboot为"无需配置(很少配置)的SSM框架",它可以帮助我们简单快速的搭建项目   相对于传统的Spring Web项目,SpringBoot:
  •  不需要配置web.xml,加载Spring和Spring MVC
  •  不需要配置数据连接,日志文件
  •  不需要加载配置文件的读取
  •  ....
  2.入门程序   ①我们可以使用STS,IDEA快速搭建Spring Boot项目,也可以在eclipse上下载SpringBoot的插件,或者进入Spring的官网(https://start.spring.io/)下载一个SpringBoot程序,在这里我们使用IDEA来创建一个简单的SpringBoot程序  一、SpringBoot入门 随笔 第1张

②,

一、SpringBoot入门 随笔 第2张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 

③,

一、SpringBoot入门 随笔 第3张

 

④.之后,弹出框选择"完成"即可,如果你以前没有运行过SpringBoot的程序,那么此时稍等一会儿,让Maven把SpringBoot组件下载到本地 完整的SpringBoot项目如下所示:(以前的图找不到了,不过项目结构大致如下,是一个maven项目)  一、SpringBoot入门 随笔 第4张

 

 

⑥.打开SpringBootApplication(或者 你的项目名+Application),查看代码
 1 package com.hnnz.demo;
 2  
 3  
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6  
 7  
 8 @SpringBootApplication
 9 public class SpringBootDemoApplication {
10  
11  
12     public static void main(String[] args) {
13         SpringApplication.run(SpringBootDemoApplication.class, args);
14     }
15  
16  
17 }

 

  • @SpringBootApplication表示这是一个SpringBoot应用程序
  • 当我们启动一个SpringBoot应用程序的时候,只需要运行这个主方法即可,因为SpringBoot内嵌了tomcat和jetty等web服务器
  • 默认情况下,我们启动的SpringBoot应用程序的端口号是8080,我们可以通过修改application,properties或者application.yml来修改端口号
⑦打开pom.xml,查看代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.1.4.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.hnnz</groupId>
12     <artifactId>demo</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>SpringBootDemo</name>
15     <description>Demo project for Spring Boot</description>
16  
17  
18     <properties>
19         <java.version>1.8</java.version>
20     </properties>
21  
22  
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28  
29  
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35     </dependencies>
36  
37  
38     <build>
39         <plugins>
40             <plugin>
41                 <groupId>org.springframework.boot</groupId>
42                 <artifactId>spring-boot-maven-plugin</artifactId>
43             </plugin>
44         </plugins>
45     </build>
46  
47  
48 </project>

 

  • 绿色标注的地方是我们SpringBoot默认继承的包
  • 红色标注的地方是给SpringBoot应用程序添加web功能
  • 紫色标注部分集成了测试功能
  • 蓝色部分是SpringBoot的Maven插件
⑧启动SpringBootDemoApplication类的主方法(确保8080端口未被占用) 控制台打印如下: 一、SpringBoot入门 随笔 第5张 打开浏览器,访问http://localhost:8080   这时返回的视图是一个错误页面,这是正常的. 一、SpringBoot入门 随笔 第6张   ⑨,添加一个Controller 一、SpringBoot入门 随笔 第7张一、SpringBoot入门 随笔 第8张   代码如下
 1 package com.hnnz.demo.controller;
 2  
 3  
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6  
 7  
 8 @RestController
 9 public class HelloController {
10  
11  
12     @RequestMapping("/hello")
13     public String hello(){
14         return "Hello,Spring Boot!";
15     }
16  
17  
18 }
  • @RestController是一个组合注解,相当于@ResponseBody+@Controller
⑩,重新启动SpringBootDemoApplication类的主方法 打开浏览器,访问http://localhost:8080/hello 一、SpringBoot入门 随笔 第9张一、SpringBoot入门 随笔 第10张   可以看到,成功的返回了正确的字符串
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄