Springboot概述
什么是: Spring Boot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品
作用:帮助开发人员快速的构建庞大的spring项目,并且尽可能的减少一切xml配置,做到开箱即用,迅速上手,让开发人员关注业务而非配置
设计目的:用来简化 Spring 应用的初始搭建以及开发过程
优点: 简单、快速、方便
用springboot搭建一个web工程
步骤
- 创建Maven工程
- 添加依赖(springboot父工程依赖 , web启动器依赖)
- 编写启动引导类(springboot项目运行的入口)
- 编写处理器Controller
- 启动项目
创建Maven工程
给pom添加依赖
1.springboot父工程依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
2.web启动器依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.配置jdk
<properties>
<java.version>1.8</java.version>
</properties>
编写启动引导类
package com.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
编写controller
package com.xxx.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
return "hello spring boot!!" ;
}
}
启动测试
运行启动类的main方法 :
控制台输出
通过输出的日志我们知道了以下信息 :
1.监听的端口是8080
2.项目的上下文路径是""
打开浏览器,访问:http://localhost:8080/hello