什么是Spring Boot
让Spring应用开发变得"简单粗暴"
Spring应用开发流程
Spring Boot应用开发流程
Spring Boot核心特性
极低的学习成本
可独立运行的Spring项目 打包为jar包进行部署
“习惯优于配置”,极大的提高了开发效率 大量使用注解方式开发
极简单的组件依赖,自动发现与自动装配
提供运行时应用监控
与分布式架构和云计算的天然集成
Maven构建Spring Boot应用
环境准备
安装JDK8以上版本
安装Intellij IDEA Ultimate (旗舰版)
Spring Boot目录结构
Spring Boot入口类
入口类命名通常以*Application结尾
入口类上增加@SpringBootApplication注解
利用SpringApplication.run()方法启动应用
Spring Boot启动流程
Spring Boot中的常用配置
application.properties核心配置文件
#修改端口号
server.port=80
#指定文件上下文
server.servlet.context-path=/
#日志输出路径
logging.file=e:myspringboot.log
#提高日志输出级别
#debug>info>warn>error>fatal
logging.level.root=debug
#自动调整到debug
debug=true
#设置jdbc数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Drivaer
spring.datasource.url=jdbc:mysql://47.114.2.235:3306/jdbc
spring.datasource.username=jdbc
spring.datasource.password=jdbc
Spring Boot支持两种配置文件
属性文件:application.properties
YAML格式:application.yml
YAML的语法
YAML是-种简洁的非标记语言。YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读。-----和json相似
YAML语法格式
标准格式🔑(空格)value
使用空格代表层级关系,以":"结束
#开启debug模式
debug: true
#logging.level.root
logging:
level:
root: info
file: e:/myspringboot.log
spring:
datasource:
driver-class-name: com.mysql.jdbc.Drivaer
url: jdbc:mysql://47.114.2.235:3306/jdbc
username: jdbc
password: jdbc
Spring Boot自定义配置项
Spring Boot允许我们自定义应用配置项,在程序运行时允许动态加载,这为程序提供了良好的可维护性。
在实际项目开发中,我们通常将项目的自定义信息放在配置文件中。
自定义配置项
mail:
config:
name: 在线考试系统
description: 这是一个自主开发的在线考试系统
hot-sales: 20
show-advert: true
程序读取
package fun.afterglow.springboottest.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
// @Value启动时自动扫描yml文件
@Value("${mall.config.name}")
private String name;
@Value("${mall.config.description}")
private String description;
@Value("${mall.config.hot-sales}")
private Integer hotSales;
@Value("${mall.config.show-advert}")
private Boolean showAdvert;
@RequestMapping("/info")
@ResponseBody
public String info(){
return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s",
name,description,hotSales,showAdvert);
}
}
环境配置文件
Spring Boot可针对不同的环境提供不同的Profile文件。
Profile文件的默认命名格式为application-{env}.yml
使用spring.profiles.active选项来指定不同的profile。
- application.yml主配置文件
#使用开发环境
spring:
profiles:
active: dev
#使用生产环境
spring:
profiles:
active: prd
- application-dev.yml开发配置文件
#开启debug模式
debug: true
#logging.level.root
logging:
level:
root: info
file: e:/myspringboot.log
spring:
datasource:
driver-class-name: com.mysql.jdbc.Drivaer
url: jdbc:mysql://47.114.2.235:3306/jdbc
username: jdbc
password: jdbc
mall:
config:
name: 在线考试系统
description: 这是一个自主开发的在线考试系统
hot-sales: 20
show-advert: true
- application-prd.yml生产环境配置文件
#开启debug模式
debug: false
#logging.level.root
logging:
level:
root: info
file: /load/uesr/app-prd.log
spring:
datasource:
driver-class-name: com.mysql.jdbc.Drivaer
url: jdbc:mysql://47.114.2.235:3306/jdbc
username: jdbc
password: jdbc
mall:
config:
name: OES
description: 这是一个自主开发的OES
hot-sales: 20
show-advert: true
server:
port: 80
打包与运行
利用Maven的package命令,生成可独立运行的Jar包。
利用java -jar xxx.jar命令启动Spring Boot应用。
Jar包可自动加载同目录的application配置文件。
- 使用:java -jar jar包名称