文章目录
- springboot
- 快速入门
- 快速构建SpringBoot工程
- 起步依赖原理分析
- springboot配置
- 配置文件分类
- yaml的基本语法
- yaml数据格式
- 获取数据
- profile
- 内部配置加载顺序
- 外部配置加载顺序
- springboot整合
- 整合junit
- 整合redis
- 整合mybatis
- springboot原理分析
- springboot自动配置
- Condition
- 监听机制
- 启动流程分析
- springboot监控
- 项目部署
- git
- mybatis-plus
- 项目实战
springboot
快速入门
配置pom.xml文件
官网获取https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/getting-started-installing-spring-boot.html#getting-started-maven-installation
<?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>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<!--springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<!--web开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
书写一个控制类
package com.controll;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class hellocontroller {
@RequestMapping("/hello")
public String hello(){
return "hello spring boot";
}
}
书写一个引导类
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class helloapplication {
public static void main(String[] args) {
SpringApplication.run(helloapplication.class,args);
}
}
在引导类文件中启动项目。
快速构建SpringBoot工程
https://www.bilibili.com/video/BV1Lq4y1J77x?p=5&vd_source=fce42173caa30698e3df32059b6f9232
起步依赖原理分析
- spring-boot-starter-parent
- spring-boot-starter-web
springboot配置
配置文件分类
yaml的基本语法
简洁,以数据为核心
yaml数据格式
server:
port: 8082
name: abc
#对象
person:
name: zhangsan
age: 20
#对象行内写法
person2: {name: zhangsan,age: 20}
#数组
address:
- beijing
- shanghai
#数组行内写法
address2: [beijing,shanghai]
#纯量
msg1: 'hello \n world' #不会识别转义字符,会原样输出
msg2: "hello \n world" #会识别转义字符
获取数据
@RestController
public class hellocontroller {
@Value("${name}")//第一种方法
private String name;
@Value("${msg1}")//纯量
private String msg1;
@Value("${msg2}")
private String msg2;
@Value("${person.name}")
private String name2;
@Value("${address[0]}")//数组
private String addresss;
@Value("${person.age}")
private int age;
@Autowired//第二种方法
private Environment env;
@Autowired//第三种方法
private person person;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(addresss);
System.out.println(msg1);
System.out.println(msg2);
System.out.println(name2);
System.out.println(age);
System.out.println("------------------------");
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[0]"));
System.out.println("---------------------");
System.out.println(person);
return "hello spring boot";
}
@RequestMapping("/hello")
public String hello(){
return "hello spring boot";
}
}
perosn:
@Component
@ConfigurationProperties(prefix="person")
public class person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
profile
激活后缀名为pro的配置文件
--- #可以分割为不同几个区域
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
---
server:
port: 8083
spring:
profiles: pro
---
spring:
profiles:
active: pro
虚拟机和程序参数也可以激活
虚拟机:-Dspring.profiles.active=pro
程序参数:--spring.profiles.active=test
内部配置加载顺序
外部配置加载顺序
视频链接
通过官网查看优先级
https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-external-config.html
springboot整合
整合junit
整合redis
整合mybatis
springboot原理分析
springboot自动配置
Condition
链接: 视频链接