SpringBoot 学习一
1.什么是 SpringBoot
SpringBoot 是 Spring 项目中的一个子工程,与我们所熟知的 Spring-framework 同属于spring 的产品
2.为什么要学习 SpringBoot
java 一直被人诟病的一点就是臃肿、麻烦。当我们还在辛苦的搭建项目时,可能 Python程序员已经把功能写好了,原因注意是两点:
- 复杂的配置
- 混乱的依赖管理
3.SpringBoot的特点
- 为所有 Spring 的开发者提供一个非常快速的、广泛接受的入门体验
- 开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),但通过自己设置参数(.properties),即可快速摆脱这种方式。
- 提供了一些大型项目中常见的非功能性特性,如内嵌服务器、安全、指标,健康检测、外部化配置等
- 绝对没有代码生成,也无需 XML 配置。
4.创建工程
4.1 创建 maven 工程
4.2.添加依赖
- 看到这里可能有疑惑,前面说传统开发的问题之一就是依赖管理混乱,怎么这里我们还需要管理依赖呢?难道 SpringBoot 不帮我们管理吗?
- 这是因为 SpringBoot 提供了一个名为 spring-boot-starter-parent 的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理,我们的项目需要以这个项目为父工程,这样我们就不用操心依赖的版本问题了,需要什么依赖,直接引入坐标即可!
4.2.1.添加父工程坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
4.2.2.添加web启动器
为了让 SpringBoot 帮我们完成各种自动配置,我们必须引入SpringBoot 提供的自动配置依赖,我们称为启动器。因为我们是web项目,这里我们引入web启动器:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4.2.3. 管理 jdk 版本
<properties>
<java.version>1.8</java.version>
</properties>
4.3.完整 demo
demo 项目结构
HelloControlller
@RestController
@RequestMapping("hello")
public class HelloControlller {
@GetMapping("show")
public String test(){
return "hello SpringBoot!";
}
}
HeController
@RestController
@RequestMapping("hello2")
public class HeController {
@GetMapping("show")
public String test(){
return "hello SpringBoot2!";
}
}
启动类
@EnableAutoConfiguration
@ComponentScan //类似<context:component-scan base-package=""> 扫描该类所在的包以及它的子子孙孙包
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
}