springboot学习第一课
主要学习内容
1. 简单介绍springboot
2. spingboot 基本配置
3. spingboot 自定义配置
4. springboot的yml配置
一. 简单介绍springboot
1.什么是springboot
springboot基于spring4.0,内嵌web容器(tomcat/jetty)的可执行程序(jar)框架
2.springboot
3.springboot
1.配置变简单
2.编码简单
3.部署简单
4.监控变简单
- 启动类注解
@SpringBootApplication
5.默认springboot的端口: 8080
6.controller上面加一个: @RestController
二:spingboot 基本配置
1) 课程目标
- 讲解resources包下的application.properties使用
2) 改端口
在 application.properties 改端口
server.port=9090
3) 改随机端口
思考问题:问什么要改随机端口
- 如果在同一台服务器上,多个服务用同一个端口,会造成端口冲突
- 在现实的开发中,开发人员不记住ip 和 端口
server.port=${random.int[1024,9999]}
三:spingboot 自定义配置
自定义配置,讲解@value注解
@value作用: 简化读取properties文件中的配置,spring支持@value注解方式来获取
aaa.msg=hi.hello.world
@RestController
public class HelloController {
@Value("${aaa.msg}")
private String msg;
@GetMapping("/msg")
public String getMsg(){
return msg;
}
}
四 springboot的yml配置
1) 学习yml个格式和yml的语法
springboot的2中配置文件方式
1.application.properties
2.application.yml
配置文件默认放在/src/main/resources目录下
2) 什么是yml
yml 以数据为中心,比json xml更适合做配置文件
3) 对比区别
server.port=9090
agan.msg=hi.hello.world
server:
port: 9090
agan:
msg: hi.hello.world
用空格的缩进程度来控制层级关系,空格多少不重要