springboot细节回顾


配置文件模块

 

.properties:目前官方不推荐,建议更换为yaml

  语法结构: key=value

 

.yaml:YamlPropertiesFactoryBean将yaml加载为Properties,YamlMapFactoryBean将yaml加载为Map,对空格要求极为严格

  语法结构:key:空格+value

##yaml

# 普通的key-value
name: tian

# 对象
student:
    name: tian
    age: 5

#行内写法
 student: {name: tian,age: 5}

# 数组
pets:
    - cat
    - dog
    - pig

pets: [cat,dog,pig]


##.properties   只能保存键值对

name=tian
student.name = tian
student.age = 5

 

.yaml多配置文件切换

server:
  port: 8081
spring:
  profiles:
    active: dev

---
server:
  port: 8082
spring:
  profiles: dev

---
server:
  port: 8083
spring:
  profiles: test

.yaml与.properties功能对比

JSR303数据校验

配置文件存放处与优先级大小

1:file: ./config/      (需要新建config文件夹)

2:file: ./

3:classpath: /config/  (需要新建config文件夹)

4:classpath:/      (项目默认存放config文件处)

配置文件server标签对应以下地址的class,同理其它

自动装配原理总结:

1:SpringBoot启动会加载大量的自动配置类

2:我们看我们需要的功能有没有在默认写好的自动配置类中

3:确认自动默认配置类中配置了哪些组件(若需要的组件在其中,就不需要手动配置了)

4:给容器中自动配置类添加组件时,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可

  xxxxAutoConfiguration:自动配置类

  xxxxProperties:封装配置文件中相关属性

5:可以通过debug=true观看哪些配置类生效,哪些没生效