SpringBoot入门(一)
1.什么是SpringBoot?
2.为什么要学习SpringBoot?
3.用两种方式创建SpringBoot项目
4.SpringMVC和SpringBoot之间的关系
5.SpringBoot的特点
6.使用SpringBoot获得参数,内置对象
7.SpringBoot的属性文件
什么是SpringBoot?
SpringBoot是Spring的一个子框架。
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
SpringBoot可以很容易的创建一个单例,以Spring为基础的产品级的应用。你只需要运行就可以了。
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的平台和第三方的库,开启应用。大多数的springboot的应用需要很少的配置。
SpringBoot依赖Spring的平台和第三方的库,可以快速的创建Spring的项目,且“零”配置。
为什么要学习SpringBoot?
我们之前创建Spring的项目,需要很多的配置,还需要使用很多的依赖。而我们springBoot从这两个角度进行了简化。
用两种方式创建SpringBoot项目
方式一:
- 创建一个maven项目
- 添加一个父依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent>
- 添加web依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- 设定jdk的版本
<properties> <java.version>1.8</java.version> </properties>
- 创建一个Springboot的启动类
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
- 写一个controller
@RestController public class HelloController { @GetMapping("/hello") public String Hello(){ return "hello spring boot"; } }
方式二:
1创建SpringInit项目,选择web依赖
2.创建Controller
@RestController public class Hello02Controller { @GetMapping("/hello02") public String hello02(){ return "德丽莎"; } }
SpringMVC和SpringBoot之间的关系
SpringBoot是Spring的升级?不是。SpringBoot只是改变了使用Spring的方式。
SpringBoot的特点
- 化繁为简(编码简单,配置简单,部署简单,监控简单)
- 备受关注的新一代框架
- 微服务的入门级框架(SSM-->SpringBoot-->SpringCloud)
使用SpringBoot获得参数,内置对象
//http://localhost:8080/bbb?cid=1 @GetMapping("/getParam") public String getParam(@RequestParam("id") int id){ System.out.println("id="+id); return "德丽莎"; } //使用restful风格访问 //http://localhost:8080/bbb/1 @GetMapping("/ccc/{id}") public String getParam02(@PathVariable int id){ System.out.println("id="+id); return "德丽莎"; } @GetMapping("/ddd") public String getParam03(HttpServletRequest request, HttpServletResponse response){ System.out.println(request+"===="+response); return "德丽莎"; }
SpringBoot的属性文件
修改端口 server.port=8081
修改访问路径 server.servlet.context-path=/Asuna
是推荐方式
总结
SpringBoot是Spring的一个子框架。SpringBoot并不是spring的升级,而是改变使用的方式。它化繁为简,是微服务入门框架,是springcloud的基础。它能够快速创建Spring项目,不需要很多的依赖,而使用启动器,不需要配置文件,基本“零”配置。
Springboot项目继承parent启动器,使用web支持的时候,使用web启动器。Springboot是基于jdk1.8及以上。它需要一个启动类,其他内容之前springmvc的一致。比如获得参数用到@getMapping @RequestParam @pathVariable。
Springboot支持使用yml的属性文件,可以配置参数和访问路径。