一、SpringBoot快速入门
需求
1.需求SpringBoot工程,定义HelloController.hell()方法,返回“Hello SpringBoot!”
实现步骤:
2.实现步骤
(1)创建Maven项目
(2)导入SpringBoot起步依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springbootLearn</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
(3)定义Controller
package com.aotao.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloSpringBoot {
@RequestMapping("/hello")
public String hell0(){
return "Hello Spirng";
}
public static void main(String[] args) {
}
}
(4)编写引导类
package com.aotao;
import com.aotao.controller.HelloSpringBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
* 引导类。springboot项目的入口
* */
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBoot.class,args);
}
}
(5)启动测试
二、SpirngBoot配置
配置文件分类
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的化,就可以使用application.porperties或者application.yml(application.yaml)进行配置
- properties:
server.prot=8080
- yml:
server:
port: 8080
YAML:基本语法
- 大小写敏感
- 数据值前边必须有空格,作为分隔符
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格(Idea中按tab自动转换成空格)
- 缩进的空格数目不重要,只要相同层级的元素左对齐即可
- #表示注释
server:
port: 8080
address: localhost
name: abc
YAML:数据格式
- 对象(map):键值对的集合
persion:
name: zhangsan
#行内写法
person: {name: zhangsan}
- 数组:一组按次序 排列的值
address:
- beijing
- shanghai
#行内写法
address: [beijing,shanghai]
- 纯量:单个的、不可再分的值
msg1: 'hello \n world' #单引号忽略转义字符
msg2: "hello \n world" #双引号识别转移字符
YMAL:参数引用
name: lisi
person:
name: ${name}
读取配置内容
1) @Value
2) Environment
3) @ConfigurationProperties
profile
如果每次打包时,都要修改配置文件,那么非常麻烦。proflie功能是进行动态配置切换的。
1)profile配置方式
- 多profile文件方式
创建三个properties后缀的文件分别时生产模式,开发模式,测试模式
文件名:application-dev.properties application-pro.properties appliaction-test.properties
写入对应文件:server.port=8081 server.prot=8083 server.port=8082
启动SpringbootProfilesApplication类,显示没有活跃的profile设置
设置活跃的profile:
在application.properties文件中写入
spring.profiles.active=dev (dev为配置文件“-”后面的后缀)
启动后:
- yml多文档方式
格式:“---”为一种类型的分解,最后一个是激活配置
---
server:
port:8081
spring:
profiles:dev
---
server:
port:8082
spring:
profiles:test
---
server:
port:8083
spring:
profiles:pro
---
spring:
profiles:
active: pro
2)profile激活方式
- 配置文件
- 虚拟机参数
- 命令行参数
内部配置的加载顺序
Springboot程序启动时,会从以下位置加载配置文件:
1. file:./config/: 当前项目下的/config目录下
2. file:./ : 当前项目的根目录
3. classpath:./config/ : classpath的/config目录
4. classpath:/ : classpath的根目录
加载顺序为上文的排序顺序,高优先级的配置属性会生效
三、SpringBoot整合其他框架
整合Redis